diff --git a/app.py b/app.py index 90b8c93..5cabda2 100644 --- a/app.py +++ b/app.py @@ -10,59 +10,60 @@ root_api_url = 'http://{}/local'.format(sever) base_path = '/app/data/' print("API_ROOT: " + str(root_api_url)) +time.sleep(900) -#try: -while True: - r = requests.get('{}/anonymize'.format(root_api_url)) - face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') - print(r.text) - response = r.json() +try: + while True: + r = requests.get('{}/anonymize'.format(root_api_url)) + face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') + print(r.text) + response = r.json() - for detection in response: - img_path = "{}{}".format(base_path, detection['source_image_path']) + for detection in response: + img_path = "{}{}".format(base_path, detection['source_image_path']) - #Skip Non existing files - if (not os.path.exists(img_path)): - continue + #Skip Non existing files + if (not os.path.exists(img_path)): + continue - img = cv2.imread(img_path) - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - faces, rejects, weights = face_cascade.detectMultiScale3(gray, 1.1, 4, outputRejectLevels = 1) + img = cv2.imread(img_path) + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + faces, rejects, weights = face_cascade.detectMultiScale3(gray, 1.1, 4, outputRejectLevels = 1) - #Count faces is null dont anonimize file - if (len(faces) < 0): - continue + #Count faces is null dont anonimize file + if (len(faces) < 0): + continue + + i = 0 + for (x, y, w, h) in faces: + confidence = float(weights[i]) + if (confidence > treashold): + face_roi = img[y:y+h, x:x+w] + blurred_face = cv2.GaussianBlur(face_roi, (99, 99), 50) + img[y:y+h, x:x+w] = blurred_face + + print(weights[i]) + text = "{}".format(weights[i]) + #cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) + #cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) + + i = i +1 + + extension = img_path[img_path.index(".")+1:] + new_img_path = img_path.replace((".{}".format(extension)), ('_anonym.{}'.format(extension))) + cv2.imwrite(new_img_path, img) + + data = { + "id": detection['id'], + "source_image_path": new_img_path.replace(base_path,"") + } + response = requests.post('{}/anonymize'.format(root_api_url), json=data) - i = 0 - for (x, y, w, h) in faces: - confidence = float(weights[i]) - if (confidence > treashold): - face_roi = img[y:y+h, x:x+w] - blurred_face = cv2.GaussianBlur(face_roi, (99, 99), 50) - img[y:y+h, x:x+w] = blurred_face + time.sleep(900) - print(weights[i]) - text = "{}".format(weights[i]) - #cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) - #cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) - - i = i +1 - - extension = img_path[img_path.index(".")+1:] - new_img_path = img_path.replace((".{}".format(extension)), ('_anonym.{}'.format(extension))) - cv2.imwrite(new_img_path, img) - - data = { - "id": detection['id'], - "source_image_path": new_img_path.replace(base_path,"") - } - response = requests.post('{}/anonymize'.format(root_api_url), json=data) +except Exception as e: + print("An exception occurred") + print(str(e)) - time.sleep(900) - -# except Exception as e: -# print("An exception occurred") -# print(str(e)) - -# data = {"trace": str(e)} -# response = requests.post('{}/log'.format(root_api_url), json=data) \ No newline at end of file + data = {"trace": str(e)} + response = requests.post('{}/log'.format(root_api_url), json=data) \ No newline at end of file