Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4d9d332

Browse files
committed
updated face detection tutorial to use SSDs as well
1 parent15e18cb commit4d9d332

File tree

5 files changed

+1878
-0
lines changed

5 files changed

+1878
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
importcv2
2+
importnumpyasnp
3+
4+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
5+
prototxt_path="weights/deploy.prototxt.txt"
6+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
7+
model_path="weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
8+
9+
# load Caffe model
10+
model=cv2.dnn.readNetFromCaffe(prototxt_path,model_path)
11+
12+
# read the desired image
13+
image=cv2.imread("kids.jpg")
14+
# get width and height of the image
15+
h,w=image.shape[:2]
16+
17+
# preprocess the image: resize and performs mean subtraction
18+
blob=cv2.dnn.blobFromImage(image,1.0, (300,300), (104.0,177.0,123.0))
19+
# set the image into the input of the neural network
20+
model.setInput(blob)
21+
# perform inference and get the result
22+
output=np.squeeze(model.forward())
23+
font_scale=1.0
24+
foriinrange(0,output.shape[0]):
25+
# get the confidence
26+
confidence=output[i,2]
27+
# if confidence is above 50%, then draw the surrounding box
28+
ifconfidence>0.5:
29+
# get the surrounding box cordinates and upscale them to original image
30+
box=output[i,3:7]*np.array([w,h,w,h])
31+
# convert to integers
32+
start_x,start_y,end_x,end_y=box.astype(np.int)
33+
# draw the rectangle surrounding the face
34+
cv2.rectangle(image, (start_x,start_y), (end_x,end_y),color=(255,0,0),thickness=2)
35+
# draw text as well
36+
cv2.putText(image,f"{confidence*100:.2f}%", (start_x,start_y-5),cv2.FONT_HERSHEY_SIMPLEX,font_scale, (255,0,0),2)
37+
# show the image
38+
cv2.imshow("image",image)
39+
cv2.waitKey(0)
40+
# save the image with rectangles
41+
cv2.imwrite("kids_detected_dnn.jpg",image)
Loading
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
importcv2
2+
importnumpyasnp
3+
4+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
5+
prototxt_path="weights/deploy.prototxt.txt"
6+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
7+
model_path="weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
8+
9+
# load Caffe model
10+
model=cv2.dnn.readNetFromCaffe(prototxt_path,model_path)
11+
12+
cap=cv2.VideoCapture(0)
13+
14+
whileTrue:
15+
16+
# read the desired image
17+
_,image=cap.read()
18+
# get width and height of the image
19+
h,w=image.shape[:2]
20+
21+
# preprocess the image: resize and performs mean subtraction
22+
blob=cv2.dnn.blobFromImage(image,1.0, (300,300), (104.0,177.0,123.0))
23+
# set the image into the input of the neural network
24+
model.setInput(blob)
25+
# perform inference and get the result
26+
output=np.squeeze(model.forward())
27+
font_scale=1.0
28+
foriinrange(0,output.shape[0]):
29+
# get the confidence
30+
confidence=output[i,2]
31+
# if confidence is above 50%, then draw the surrounding box
32+
ifconfidence>0.5:
33+
# get the surrounding box cordinates and upscale them to original image
34+
box=output[i,3:7]*np.array([w,h,w,h])
35+
# convert to integers
36+
start_x,start_y,end_x,end_y=box.astype(np.int)
37+
# draw the rectangle surrounding the face
38+
cv2.rectangle(image, (start_x,start_y), (end_x,end_y),color=(255,0,0),thickness=2)
39+
# draw text as well
40+
cv2.putText(image,f"{confidence*100:.2f}%", (start_x,start_y-5),cv2.FONT_HERSHEY_SIMPLEX,font_scale, (255,0,0),2)
41+
# show the image
42+
cv2.imshow("image",image)
43+
ifcv2.waitKey(1)==ord("q"):
44+
break
45+
46+
cv2.destroyAllWindows()
47+
cap.release()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp