Update app.py
Browse files
app.py
CHANGED
@@ -4,101 +4,89 @@ import fast_colorthief
|
|
4 |
import webcolors
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
def Detection(filename):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
t=str(err)
|
94 |
-
if t.__contains__(error):
|
95 |
-
break
|
96 |
-
|
97 |
-
print(FinalItems)
|
98 |
-
return str(FinalItems)
|
99 |
-
|
100 |
-
interface = gr.Interface(fn=Detection,
|
101 |
-
inputs=["video"],
|
102 |
-
outputs="text",
|
103 |
-
title='Object & Color Detection in Video')
|
104 |
-
interface.launch(inline=False,debug=True)
|
|
|
4 |
import webcolors
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
+
|
8 |
+
thres = 0.45 # Threshold to detect object
|
9 |
+
|
10 |
+
|
11 |
+
def Detection(filename, confidence_threshold=0.45):
|
12 |
+
cap = cv2.VideoCapture(filename)
|
13 |
+
framecount = 0
|
14 |
+
|
15 |
+
cap.set(3, 1280)
|
16 |
+
cap.set(4, 720)
|
17 |
+
cap.set(10, 70)
|
18 |
+
|
19 |
+
error = "in function 'cv::imshow'"
|
20 |
+
classNames = []
|
21 |
+
FinalItems = []
|
22 |
+
classFile = 'coco.names'
|
23 |
+
with open(classFile, 'rt') as f:
|
24 |
+
classNames = f.readlines()
|
25 |
+
|
26 |
+
# remove new line characters
|
27 |
+
classNames = [x.strip() for x in classNames]
|
28 |
+
|
29 |
+
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
|
30 |
+
weightsPath = 'frozen_inference_graph.pb'
|
31 |
+
|
32 |
+
net = cv2.dnn_DetectionModel(weightsPath, configPath)
|
33 |
+
net.setInputSize(320, 320)
|
34 |
+
net.setInputScale(1.0 / 127.5)
|
35 |
+
net.setInputMean((127.5, 127.5, 127.5))
|
36 |
+
net.setInputSwapRB(True)
|
37 |
+
|
38 |
+
while True:
|
39 |
+
success, img = cap.read()
|
40 |
+
|
41 |
+
# #Colour
|
42 |
+
try:
|
43 |
+
image = Image.fromarray(img)
|
44 |
+
image = image.convert('RGBA')
|
45 |
+
image = np.array(image).astype(np.uint8)
|
46 |
+
palette = fast_colorthief.get_palette(image)
|
47 |
+
|
48 |
+
for i in range(len(palette)):
|
49 |
+
diff = {}
|
50 |
+
for color_hex, color_name in webcolors.CSS3_HEX_TO_NAMES.items():
|
51 |
+
r, g, b = webcolors.hex_to_rgb(color_hex)
|
52 |
+
diff[sum([(r - palette[i][0]) ** 2,
|
53 |
+
(g - palette[i][1]) ** 2,
|
54 |
+
(b - palette[i][2]) ** 2])] = color_name
|
55 |
+
if FinalItems.count(diff[min(diff.keys())]) == 0:
|
56 |
+
FinalItems.append(diff[min(diff.keys())])
|
57 |
+
|
58 |
+
except:
|
59 |
+
pass
|
60 |
+
|
61 |
+
try:
|
62 |
+
classIds, confs, bbox = net.detect(
|
63 |
+
img, confThreshold=confidence_threshold)
|
64 |
+
except:
|
65 |
+
pass
|
66 |
+
|
67 |
+
try:
|
68 |
+
if len(classIds) != 0:
|
69 |
+
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
|
70 |
+
if FinalItems.count(classNames[classId - 1]) == 0:
|
71 |
+
FinalItems.append(classNames[classId - 1])
|
72 |
+
|
73 |
+
if framecount > cap.get(cv2.CAP_PROP_FRAME_COUNT):
|
74 |
+
break
|
75 |
+
else:
|
76 |
+
framecount += 1
|
77 |
+
except Exception as err:
|
78 |
+
print(err)
|
79 |
+
t = str(err)
|
80 |
+
if t.__contains__(error):
|
81 |
+
break
|
82 |
+
|
83 |
+
print(FinalItems)
|
84 |
+
return str(FinalItems)
|
85 |
+
|
86 |
+
|
87 |
+
interface = gr.Interface(fn=Detection,
|
88 |
+
inputs=["video", gr.inputs.Slider(0.01, 1, step=0.01, label="Limiar de Confiança")],
|
89 |
+
outputs="text",
|
90 |
+
title='Detecção de Objetos e Cores em Vídeo',
|
91 |
+
description='Este aplicativo detecta objetos em um vídeo e identifica suas cores. Carregue um vídeo e ajuste o limiar de confiança para a detecção de objetos.')
|
92 |
+
interface.launch(inline=False, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|