Skip to content

Instantly share code, notes, and snippets.

@igorrendulic
Created September 9, 2020 23:50
Show Gist options
  • Save igorrendulic/1a85439ba62a4c29e2824c89b663f38c to your computer and use it in GitHub Desktop.
Save igorrendulic/1a85439ba62a4c29e2824c89b663f38c to your computer and use it in GitHub Desktop.
def validate_param(param_name):
"""
Validating OS environment variables
"""
if param_name not in os.environ:
raise ValueError("missing environment variable " + param_name)
return os.environ[param_name]
if __name__ == "__main__":
port = validate_param('chrys_port')
host = validate_param('chrys_host')
password = validate_param('chrys_password')
cert_path = validate_param('chrys_cert')
chrys = chrysalis.Connect(host=host, port=port, password=password, ssl_ca_cert=cert_path)
print("[INFO] loading face detector model...")
prototxtPath ="deploy.prototxt"
weightsPath = "res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
print("[INFO] loading face mask detector model...")
maskNet = load_model("mask_detector.model")
# initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
while True:
img = chrys.VideoLatestImage()
if img is not None:
frame = img.data
frame = imutils.resize(frame, width=400)
# detect faces in the frame and determine if they are wearing a
# face mask or not
(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
# loop over the detected face locations and their corresponding locations
for (box, pred) in zip(locs, preds):
# unpack the bounding box and predictions
(startX, startY, endX, endY) = box
(mask, withoutMask) = pred
# determine the class label and color we'll use to draw
# the bounding box and text
label = "Mask" if mask > withoutMask else "No Mask"
color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
# include the probability in the label
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
# display the label and bounding box rectangle on the output frame
cv2.putText(frame, label, (startX, startY - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
# show the output frame
cv2.imshow("mask_detector", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment