1
+ from imutils .video import VideoStream
2
+ import numpy as np
3
+ import argparse
4
+ import imutils
5
+ import time
6
+ import cv2
7
+
8
+ ap = argparse .ArgumentParser ()
9
+ ap .add_argument ("-p" , "--prototxt" , required = True ,
10
+ help = "path to Caffe 'deploy' prototxt file" )
11
+ ap .add_argument ("-m" , "--model" , required = True ,
12
+ help = "path to Caffe pre-trained model" )
13
+ ap .add_argument ("-c" , "--confidence" , type = float , default = 0.5 ,
14
+ help = "minimum probability to filter weak detections" )
15
+ args = vars (ap .parse_args ())
16
+
17
+ print ("[INFO] loading model..." )
18
+ net = cv2 .dnn .readNetFromCaffe (args ["prototxt" ], args ["model" ])
19
+
20
+ print ("[INFO] starting video stream..." )
21
+ vs = VideoStream (src = 0 ).start ()
22
+ time .sleep (2.0 )
23
+
24
+ while True :
25
+ frame = vs .read ()
26
+ frame = imutils .resize (frame , width = 400 )
27
+
28
+ (h , w ) = frame .shape [:2 ]
29
+ blob = cv2 .dnn .blobFromImage (cv2 .resize (frame , (300 , 300 )), 1.0 ,
30
+ (300 , 300 ), (104.0 , 177.0 , 123.0 ))
31
+
32
+ net .setInput (blob )
33
+ detections = net .forward ()
34
+
35
+ for i in range (0 , detections .shape [2 ]):
36
+ confidence = detections [0 , 0 , i , 2 ]
37
+
38
+ if confidence < args ["confidence" ]:
39
+ continue
40
+
41
+ box = detections [0 , 0 , i , 3 :7 ] * np .array ([w , h , w , h ])
42
+ (startX , startY , endX , endY ) = box .astype ("int" )
43
+
44
+ text = "{:.2f}%" .format (confidence * 100 )
45
+ y = startY - 10 if startY - 10 > 10 else startY + 10
46
+ cv2 .rectangle (frame , (startX , startY ), (endX , endY ),
47
+ (0 , 0 , 255 ), 2 )
48
+ cv2 .putText (frame , text , (startX , y ),
49
+ cv2 .FONT_HERSHEY_SIMPLEX , 0.45 , (0 , 0 , 255 ), 2 )
50
+
51
+ cv2 .imshow ("Frame" , frame )
52
+ key = cv2 .waitKey (1 ) & 0xFF
53
+
54
+ if key == ord ("q" ):
55
+ break
56
+
57
+ cv2 .destroyAllWindows ()
58
+ vs .stop ()
0 commit comments