-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectDetection.py
More file actions
65 lines (54 loc) · 1.94 KB
/
Copy pathObjectDetection.py
File metadata and controls
65 lines (54 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
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
import cv2
import matplotlib.pyplot as mat
from time import sleep
config_file = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
frozen_model = "frozen_inference_graph.pb"
model = cv2.dnn_DetectionModel(frozen_model, config_file)
Bio=["person","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe""banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake"]
ClassLabels = []
fileName = "labels.txt"
with open(fileName, "rt") as fo:
ClassLabels = fo.read().rstrip("\n").split("\n")
model.setInputSize(320, 320)
model.setInputScale(1.0 / 127.5)
model.setInputMean((127.5, 127.5, 127.5))
model.setInputSwapRB(True)
cap = cv2.VideoCapture(0)
cap.set(3,720)#Width
cap.set(4,1080)#Height
cap.set(10,100)#Brightness
if not cap.isOpened():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Can't open the video")
font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
while True:
success, img = cap.read()
ClassIndex, Confidence, bbox = model.detect(img, confThreshold=0.5)
print(ClassIndex)
if len(ClassIndex) != 0:
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), Confidence.flatten(), bbox):
if ClassInd - 1 < len(ClassLabels):
label = ClassLabels[ClassInd - 1]
else:
label = 'Unknown'
if label in Bio:
type="BioDegradable"
else:
type="Non-BioDegradable"
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(
img,
type,
(boxes[0] + 10, boxes[1] + 40),
font,
fontScale=font_scale,
color=(0, 255, 0),
thickness=3,
)
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()