#!/usr/bin/env python3 import os import sys import shutil import cv2 from urllib.request import urlretrieve def download_file(url, local_filename = None): if not local_filename: local_filename = url.split('/')[-1] if os.path.exists(local_filename): return local_filename print("Downloading {local_filename}, please wait...".format(local_filename=local_filename)) urlretrieve(url, local_filename) #with requests.get(url, stream=True) as r: # r.raise_for_status() # with open(local_filename, 'wb') as f: # shutil.copyfileobj(r.raw, f) return local_filename print("== Download model and test files") model_base_url = "https://download.01.org/opencv/2020/openvinotoolkit/2020.1/open_model_zoo/models_bin/1/age-gender-recognition-retail-0013/FP16/" model_xml = download_file(model_base_url + "/age-gender-recognition-retail-0013.xml", "age-gender-recognition-retail-0013.2020.xml") model_bin = download_file(model_base_url + "/age-gender-recognition-retail-0013.bin", "age-gender-recognition-retail-0013.2020.bin") test_image = download_file("https://images.unsplash.com/photo-1583686543381-2608547b3963?w=633", "test.jpg") print("== Loading age and gender model") net2 = cv2.dnn_DetectionModel(model_xml, model_bin) # Specify target device. net2.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # Read an image. print("== Loading image: {test_image}".format(test_image=test_image)) frame = cv2.imread(test_image) if frame is None: raise Exception('Image not found!') cv2.imshow("image", frame) cv2.waitKey(0) # Perform an inference. print("== Detecting faces") _, confidences, boxes = net2.detect(frame, confThreshold=0.5) # Draw detected faces on the frame. for confidence, box in zip(list(confidences), boxes): print("{confidence} / {box}".format(confidence=confidence, box=box)) face = frame[ box[1]:box[1]+box[3], box[0]:box[0]+box[2], ] cv2.imshow("face", face) cv2.waitKey(0) cv2.rectangle(frame, box, color=(0, 255, 0)) info = net2.detect(face) print(info) # Save the frame to an image file. #cv2.imwrite('out.png', frame) cv2.imshow("image", frame) cv2.waitKey(0)