Coding
Coding
import math
import time
import argparse
frameOpencvDnn = frame.copy()
frameHeight = frameOpencvDnn.shape[0]
frameWidth = frameOpencvDnn.shape[1]
net.setInput(blob)
detections = net.forward()
bboxes = []
for i in range(detections.shape[2]):
confidence = detections[0,0,i,2]
x1 = int(detections[0,0,i,3]* frameWidth)
y1 = int(detections[0,0,i,4]* frameHeight)
x2 = int(detections[0,0,i,5]* frameWidth)
y2 = int(detections[0,0,i,6]* frameHeight)
bboxes.append([x1,y1,x2,y2])
cv2.rectangle(frameOpencvDnn,(x1,y1),(x2,y2),(255,0,255),int(round(frameHeight/150)),8)
faceProto = "opencv_face_detector.pbtxt"
faceModel = "opencv_face_detector_uint8.pb"
ageProto = "age_deploy.prototxt"
ageModel = "age_net.caffemodel"
genderProto = "gender_deploy.prototxt"
genderModel = "gender_net.caffemodel"
ageNet = cv2.dnn.readNet(ageModel,ageProto)
cap = cv2.VideoCapture(0)
padding = 20
#read frame
t = time.time()
if not hasFrame:
cv2.waitKey()
break
if not bboxes:
continue
face = small_frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),
max(0,bbox[0]-padding):min(bbox[2]+padding, frame.shape[1]-1)]
genderNet.setInput(blob)
genderPreds = genderNet.forward()
gender = genderList[genderPreds[0].argmax()]
ageNet.setInput(blob)
agePreds = ageNet.forward()
age = ageList[agePreds[0].argmax()]