Spaces:
Sleeping
Sleeping
Delete common_detection.py
Browse files- common_detection.py +0 -47
common_detection.py
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
from PIL import Image
|
4 |
-
|
5 |
-
def perform_detection(image, interpreter, labels, input_details, output_details, height, width, floating_model):
|
6 |
-
imH, imW, _ = image.shape
|
7 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
8 |
-
image_resized = cv2.resize(image_rgb, (width, height))
|
9 |
-
input_data = np.expand_dims(image_resized, axis=0)
|
10 |
-
|
11 |
-
input_mean = 127.5
|
12 |
-
input_std = 127.5
|
13 |
-
if floating_model:
|
14 |
-
input_data = (np.float32(input_data) - input_mean) / input_std
|
15 |
-
|
16 |
-
interpreter.set_tensor(input_details[0]['index'], input_data)
|
17 |
-
interpreter.invoke()
|
18 |
-
|
19 |
-
boxes = interpreter.get_tensor(output_details[0]['index'])[0]
|
20 |
-
classes = interpreter.get_tensor(output_details[1]['index'])[0]
|
21 |
-
scores = interpreter.get_tensor(output_details[2]['index'])[0]
|
22 |
-
|
23 |
-
print(f"boxes shape: {boxes.shape}")
|
24 |
-
print(f"classes shape: {classes.shape}")
|
25 |
-
print(f"scores shape: {scores.shape}")
|
26 |
-
|
27 |
-
detections = []
|
28 |
-
for i in range(len(scores)):
|
29 |
-
if scores[i] > 0.5:
|
30 |
-
ymin = int(max(1, (boxes[i][0] * imH)))
|
31 |
-
xmin = int(max(1, (boxes[i][1] * imW)))
|
32 |
-
ymax = int(min(imH, (boxes[i][2] * imH)))
|
33 |
-
xmax = int(min(imW, (boxes[i][3] * imW)))
|
34 |
-
|
35 |
-
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
|
36 |
-
object_name = labels[int(classes[i])]
|
37 |
-
label = f'{object_name}: {int(scores[i] * 100)}%'
|
38 |
-
labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
|
39 |
-
label_ymin = max(ymin, labelSize[1] + 10)
|
40 |
-
cv2.rectangle(image, (xmin, label_ymin - labelSize[1] - 10), (xmin + labelSize[0], label_ymin + baseLine - 10), (255, 255, 255), cv2.FILLED)
|
41 |
-
cv2.putText(image, label, (xmin, label_ymin - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
|
42 |
-
|
43 |
-
detections.append([object_name, scores[i], xmin, ymin, xmax, ymax])
|
44 |
-
return image
|
45 |
-
|
46 |
-
def resize_image(image, size=640):
|
47 |
-
return cv2.resize(image, (size, size))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|