DHEIVER commited on
Commit
1ea1af5
·
verified ·
1 Parent(s): d05b9de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -86
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
- 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
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)