Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created November 17, 2020 06:46
Show Gist options
  • Save amankharwal/055d74e6ec05c5ca661ba750340ac791 to your computer and use it in GitHub Desktop.
Save amankharwal/055d74e6ec05c5ca661ba750340ac791 to your computer and use it in GitHub Desktop.
test_images = ['1114.png','1504.jpg', '0072.jpg','0012.jpg','0353.jpg','1374.jpg']
gamma = 2.0
fig = plt.figure(figsize = (14,14))
rows = 3
cols = 2
axes = []
assign = {'0':'Mask','1':"No Mask"}
for j,im in enumerate(test_images):
image = cv2.imread(os.path.join(image_directory,im),1)
image = adjust_gamma(image, gamma=gamma)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300,300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
cvNet.setInput(blob)
detections = cvNet.forward()
for i in range(0, detections.shape[2]):
try:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
frame = image[startY:endY, startX:endX]
confidence = detections[0, 0, i, 2]
if confidence > 0.2:
im = cv2.resize(frame,(img_size,img_size))
im = np.array(im)/255.0
im = im.reshape(1,124,124,3)
result = model.predict(im)
if result>0.5:
label_Y = 1
else:
label_Y = 0
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.putText(image,assign[str(label_Y)] , (startX, startY-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (36,255,12), 2)
except:pass
axes.append(fig.add_subplot(rows, cols, j+1))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()
@Vgwang
Copy link

Vgwang commented Dec 10, 2021

Hello,

I was trying to follow along with your face mask detection project tutorial to further my knowledge in image recognition and I'm having trouble running this portion of the code because my confidence values are way too small to trigger the if statement on line 22. I have made sure I followed all of the rest of your code but I am still running into this issue. I have tried lowering the if statement trigger to confidence > 0 to get past the flag, but then the program does not run lines 23 to 26. Could you explain how I could solve this issue? Thank you so much for your time.

Best,
Victor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment