Skip to content

Instantly share code, notes, and snippets.

@ashhadulislam
Created June 16, 2021 07:27
Show Gist options
  • Save ashhadulislam/edf9d378b92c7f581279c5813ce3abb8 to your computer and use it in GitHub Desktop.
Save ashhadulislam/edf9d378b92c7f581279c5813ce3abb8 to your computer and use it in GitHub Desktop.
# read the saved model
def setup_hard_hat_detector():
return load_model("res/vgg16_model/ninety2_percent_just_vgg16.h5")
hh_model=setup_hard_hat_detector()
location="raw_yolo_keras/end_end_test"
#location where the images downloaded from the internet are placed
dim=(76,76)
head_taller_parameter=0.5
chin_taller_parameter=0.1
# parameters to extend the cropped faces
file_list=os.listdir(location)
for file in file_list:
frame=cv2.imread(os.path.join(location,file))
(h, w) = frame.shape[:2]
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
image = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(image)
all_wearing_hats=True
colors=[]
for f_loc in face_locations:
startY, endX, endY, startX=f_loc
height=endY-startY
#increase height by some %
taller_head=int(head_taller_parameter*height)
taller_chin=int(chin_taller_parameter*height)
startY=max(0,startY-taller_head)
endY=min(h,endY+taller_chin)
cropped_image = frame[startY:endY, startX:endX]
resized = cv2.resize(cropped_image, dim, interpolation = cv2.INTER_AREA)
lst=np.array([resized])
preds = hh_model.predict(lst)
all_wearing_hats=all_wearing_hats&(preds[0][1]>0.5)
if preds[0][1]>0.5:
colors.append((0,255,0))
else:
colors.append((255,0,0))
for i in range(len(face_locations)):
startY, endX, endY, startX=face_locations[i]
color=colors[i]
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
plt.imshow(frame)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment