Skip to content

Commit 9d53a92

Browse files
committed
Initial
0 parents  commit 9d53a92

9 files changed

+8759
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
3+
thres = 0.45 # Threshold to detect object
4+
5+
cap = cv2.VideoCapture(1)
6+
cap.set(3,1280)
7+
cap.set(4,720)
8+
cap.set(10,70)
9+
10+
classNames= []
11+
classFile = ‘coco.names’
12+
with open(classFile,’rt’) as f:
13+
classNames = f.read().rstrip(‘n’).split(‘n’)
14+
15+
configPath = ‘ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt’
16+
weightsPath = ‘frozen_inference_graph.pb’
17+
18+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
19+
net.setInputSize(320,320)
20+
net.setInputScale(1.0/ 127.5)
21+
net.setInputMean((127.5, 127.5, 127.5))
22+
net.setInputSwapRB(True)
23+
24+
while True:
25+
success,img = cap.read()
26+
classIds, confs, bbox = net.detect(img,confThreshold=thres)
27+
print(classIds,bbox)
28+
29+
if len(classIds) != 0:
30+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
31+
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
32+
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
33+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
34+
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
35+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
36+
37+
cv2.imshow(“Output”,img)
38+
cv2.waitKey(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import cv2
2+
thres = 0.45 # Threshold to detect object
3+
4+
cap = cv2.VideoCapture(1)
5+
cap.set(3,1280)
6+
cap.set(4,720)
7+
cap.set(10,70)
8+
9+
classNames= []
10+
classFile = ‘coco.names’
11+
with open(classFile,’rt’) as f:
12+
classNames = f.read().rstrip(‘n’).split(‘n’)
13+
14+
configPath = ‘ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt’
15+
weightsPath = ‘frozen_inference_graph.pb’
16+
17+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
18+
net.setInputSize(320,320)
19+
net.setInputScale(1.0/ 127.5)
20+
net.setInputMean((127.5, 127.5, 127.5))
21+
net.setInputSwapRB(True)
22+
23+
while True:
24+
success,img = cap.read()
25+
classIds, confs, bbox = net.detect(img,confThreshold=thres)
26+
print(classIds,bbox)
27+
28+
if len(classIds) != 0:
29+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
30+
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
31+
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
32+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
33+
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
34+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
35+
36+
cv2.imshow(“Output”,img)
37+
cv2.waitKey(1)

Object_Detection_Files/coco.names

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
person
2+
bicycle
3+
car
4+
motorcycle
5+
airplane
6+
bus
7+
train
8+
truck
9+
boat
10+
traffic light
11+
fire hydrant
12+
street sign
13+
stop sign
14+
parking meter
15+
bench
16+
bird
17+
cat
18+
dog
19+
horse
20+
sheep
21+
cow
22+
elephant
23+
bear
24+
zebra
25+
giraffe
26+
hat
27+
backpack
28+
umbrella
29+
shoe
30+
eye glasses
31+
handbag
32+
tie
33+
suitcase
34+
frisbee
35+
skis
36+
snowboard
37+
sports ball
38+
kite
39+
baseball bat
40+
baseball glove
41+
skateboard
42+
surfboard
43+
tennis racket
44+
bottle
45+
plate
46+
wine glass
47+
cup
48+
fork
49+
knife
50+
spoon
51+
bowl
52+
banana
53+
apple
54+
sandwich
55+
orange
56+
broccoli
57+
carrot
58+
hot dog
59+
pizza
60+
donut
61+
cake
62+
chair
63+
couch
64+
potted plant
65+
bed
66+
mirror
67+
dining table
68+
window
69+
desk
70+
toilet
71+
door
72+
tv
73+
laptop
74+
mouse
75+
remote
76+
keyboard
77+
cell phone
78+
microwave
79+
oven
80+
toaster
81+
sink
82+
refrigerator
83+
blender
84+
book
85+
clock
86+
vase
87+
scissors
88+
teddy bear
89+
hair drier
90+
toothbrush
91+
hair brush
12.8 MB
Binary file not shown.
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import cv2
2+
3+
#thres = 0.45 # Threshold to detect object
4+
5+
classNames = []
6+
classFile = "/home/debug.tsang/Documents/Object_Detection_Files/coco.names"
7+
with open(classFile,"rt") as f:
8+
classNames = f.read().rstrip("\n").split("\n")
9+
10+
configPath = "/home/debug.tsang/Documents/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
11+
weightsPath = "/home/debug.tsang/Documents/Object_Detection_Files/frozen_inference_graph.pb"
12+
13+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
14+
net.setInputSize(320,320)
15+
net.setInputScale(1.0/ 127.5)
16+
net.setInputMean((127.5, 127.5, 127.5))
17+
net.setInputSwapRB(True)
18+
19+
20+
def getObjects(img, thres, nms, draw=True, objects=[]):
21+
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
22+
#print(classIds,bbox)
23+
if len(objects) == 0: objects = classNames
24+
objectInfo =[]
25+
if len(classIds) != 0:
26+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
27+
className = classNames[classId - 1]
28+
if className in objects:
29+
objectInfo.append([box,className])
30+
if (draw):
31+
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
32+
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
33+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
34+
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
35+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
36+
37+
return img,objectInfo
38+
39+
40+
if __name__ == "__main__":
41+
42+
cap = cv2.VideoCapture(0)
43+
cap.set(3,640)
44+
cap.set(4,480)
45+
#cap.set(10,70)
46+
47+
48+
while True:
49+
success, img = cap.read()
50+
result, objectInfo = getObjects(img,0.45,0.2, objects=['person'])
51+
#print(objectInfo)
52+
cv2.imshow("Output",img)
53+
cv2.waitKey(1)
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import cv2
2+
3+
import time
4+
from gpiozero import AngularServo
5+
servo =AngularServo(18, initial_angle=0, min_pulse_width=0.0006, max_pulse_width=0.0023)
6+
7+
#thres = 0.45 # Threshold to detect object
8+
9+
classNames = []
10+
classFile = "/home/debug.tsang/Documents/Object_Detection_Files/coco.names"
11+
with open(classFile,"rt") as f:
12+
classNames = f.read().rstrip("\n").split("\n")
13+
14+
configPath = "/home/debug.tsang/Documents/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
15+
weightsPath = "/home/debug.tsang/Documents/Object_Detection_Files/frozen_inference_graph.pb"
16+
17+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
18+
net.setInputSize(320,320)
19+
net.setInputScale(1.0/ 127.5)
20+
net.setInputMean((127.5, 127.5, 127.5))
21+
net.setInputSwapRB(True)
22+
23+
24+
def getObjects(img, thres, nms, draw=True, objects=[]):
25+
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
26+
#print(classIds,bbox)
27+
if len(objects) == 0: objects = classNames
28+
objectInfo =[]
29+
if len(classIds) != 0:
30+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
31+
className = classNames[classId - 1]
32+
if className in objects:
33+
objectInfo.append([box,className])
34+
if (draw):
35+
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
36+
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
37+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
38+
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
39+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
40+
41+
servo.angle = -90
42+
time.sleep = 2
43+
servo.angle = 90
44+
45+
return img,objectInfo
46+
47+
48+
if __name__ == "__main__":
49+
50+
cap = cv2.VideoCapture(0)
51+
cap.set(3,640)
52+
cap.set(4,480)
53+
#cap.set(10,70)
54+
55+
56+
while True:
57+
success, img = cap.read()
58+
result, objectInfo = getObjects(img,0.45,0.2, objects=['cup','horse'])
59+
#print(objectInfo)
60+
61+
62+
63+
cv2.imshow("Output",img)
64+
cv2.waitKey(1)
65+
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import cv2
2+
3+
thres = 0.6 # Threshold to detect object
4+
5+
classNames = []
6+
classFile = "/home/debug.tsang/Documents/Object_Detection_Files/coco.names"
7+
with open(classFile,"rt") as f:
8+
classNames = f.read().rstrip("\n").split("\n")
9+
10+
configPath = "/home/debug.tsang/Documents/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
11+
weightsPath = "/home/debug.tsang/Documents/Object_Detection_Files/frozen_inference_graph.pb"
12+
13+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
14+
net.setInputSize(320,320)
15+
net.setInputScale(1.0/ 127.5)
16+
net.setInputMean((127.5, 127.5, 127.5))
17+
net.setInputSwapRB(True)
18+
19+
20+
def getObjects(img, thres, nms, draw=True, objects=[]):
21+
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
22+
#print(classIds,bbox)
23+
if len(objects) == 0: objects = classNames
24+
objectInfo =[]
25+
if len(classIds) != 0:
26+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
27+
className = classNames[classId - 1]
28+
if className in objects:
29+
objectInfo.append([box,className])
30+
if (draw):
31+
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
32+
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
33+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
34+
cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
35+
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
36+
37+
return img,objectInfo
38+
39+
40+
if __name__ == "__main__":
41+
42+
cap = cv2.VideoCapture(0)
43+
cap.set(3,640)
44+
cap.set(4,480)
45+
#cap.set(10,70)
46+
47+
48+
while True:
49+
success, img = cap.read()
50+
result, objectInfo = getObjects(img,thres,0.2)
51+
for i in objectInfo:
52+
#print(objectInfo)
53+
print(i[1])
54+
55+
cv2.imshow("Output",img)
56+
cv2.waitKey(1)

0 commit comments

Comments
 (0)