Update app.py
Browse files
app.py
CHANGED
@@ -4,89 +4,101 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import webcolors
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
+
thres = 0.45 # Threshold to detect object
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def Detection(filename):
|
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.read().rstrip('n').split('n')
|
25 |
+
classNames = f.readlines()
|
26 |
+
|
27 |
+
|
28 |
+
# remove new line characters
|
29 |
+
classNames = [x.strip() for x in classNames]
|
30 |
+
print(classNames)
|
31 |
+
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
|
32 |
+
weightsPath = 'frozen_inference_graph.pb'
|
33 |
+
|
34 |
+
|
35 |
+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
|
36 |
+
net.setInputSize(320,320)
|
37 |
+
net.setInputScale(1.0/ 127.5)
|
38 |
+
net.setInputMean((127.5, 127.5, 127.5))
|
39 |
+
net.setInputSwapRB(True)
|
40 |
+
|
41 |
+
while True:
|
42 |
+
success,img = cap.read()
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
# #Colour
|
47 |
+
try:
|
48 |
+
image = Image.fromarray(img)
|
49 |
+
image = image.convert('RGBA')
|
50 |
+
image = np.array(image).astype(np.uint8)
|
51 |
+
palette=fast_colorthief.get_palette(image)
|
52 |
+
|
53 |
+
|
54 |
+
for i in range(len(palette)):
|
55 |
+
diff={}
|
56 |
+
for color_hex, color_name in webcolors.CSS3_HEX_TO_NAMES.items():
|
57 |
+
r, g, b = webcolors.hex_to_rgb(color_hex)
|
58 |
+
diff[sum([(r - palette[i][0])**2,
|
59 |
+
(g - palette[i][1])**2,
|
60 |
+
(b - palette[i][2])**2])]= color_name
|
61 |
+
if FinalItems.count(diff[min(diff.keys())])==0:
|
62 |
+
FinalItems.append(diff[min(diff.keys())])
|
63 |
+
|
64 |
+
except:
|
65 |
+
pass
|
66 |
+
|
67 |
+
try:
|
68 |
+
classIds, confs, bbox = net.detect(img,confThreshold=thres)
|
69 |
+
except:
|
70 |
+
pass
|
71 |
+
print(classIds,bbox)
|
72 |
+
try:
|
73 |
+
if len(classIds) != 0:
|
74 |
+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
|
75 |
+
|
76 |
+
#cv2.rectangle(img,box,color=(0,255,0),thickness=2)
|
77 |
+
#cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
|
78 |
+
#cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
|
79 |
+
#cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
|
80 |
+
#cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
|
81 |
+
if FinalItems.count(classNames[classId-1]) == 0:
|
82 |
+
FinalItems.append(classNames[classId-1])
|
83 |
+
|
84 |
+
|
85 |
+
#cv2.imshow("Output",img)
|
86 |
+
cv2.waitKey(10)
|
87 |
+
if framecount>cap.get(cv2.CAP_PROP_FRAME_COUNT):
|
88 |
+
break
|
89 |
+
else:
|
90 |
+
framecount+=1
|
91 |
+
except Exception as err:
|
92 |
+
print(err)
|
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)
|