Skip to content
Snippets Groups Projects
Commit 7b897729 authored by Hailu, Dawit's avatar Hailu, Dawit
Browse files

Upload New File

parent c285fb61
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
from retinaface import RetinaFace
from deepface import DeepFace
import matplotlib.pyplot as plt
import cv2
import numpy as np
from utilpack.util import *
import os
import mediapipe as mp
import time
```
%% Cell type:code id: tags:
``` python
from keras.preprocessing import image
def resize_img(img,target_size,grayscale=False, return_region = False):
if img.shape[0] > 0 and img.shape[1] > 0:
factor_0 = target_size[0] / img.shape[0]
factor_1 = target_size[1] / img.shape[1]
factor = min(factor_0, factor_1)
dsize = (int(img.shape[1] * factor), int(img.shape[0] * factor))
img = cv2.resize(img, dsize)
# Then pad the other side to the target size by adding black pixels
diff_0 = target_size[0] - img.shape[0]
diff_1 = target_size[1] - img.shape[1]
if grayscale == False:
# Put the base image in the middle of the padded image
img = np.pad(img, ((diff_0 // 2, diff_0 - diff_0 // 2), (diff_1 // 2, diff_1 - diff_1 // 2), (0, 0)), 'constant')
else:
img = np.pad(img, ((diff_0 // 2, diff_0 - diff_0 // 2), (diff_1 // 2, diff_1 - diff_1 // 2)), 'constant')
#------------------------------------------
#double check: if target image is not still the same size with target.
if img.shape[0:2] != target_size:
img = cv2.resize(img, target_size)
#---------------------------------------------------
#normalizing the image pixels
img_pixels = image.img_to_array(img) #what this line doing? must?
img_pixels = np.expand_dims(img_pixels, axis = 0)
img_pixels /= 255 #normalize input in [0, 1]
#---------------------------------------------------
if return_region == True:
return img_pixels, region
else:
return img_pixels
```
%% Cell type:code id: tags:
``` python
#conda install -c conda-forge dlib
backends = ['opencv', 'ssd', 'Dlib', 'mtcnn', 'retinaface', 'mediapipe']
models = ["VGG-Face", "Facenet", "openFace", "DeepFace", "DeepID", "ArcFace", "Dlib"]
img1_path = "images/frame_screenshot_07.02.2022_1.png"
img2_path = "images/frame_screenshot_07.02.2022.png"
face = DeepFace.detectFace(img1_path,(224,224),backends[5], enforce_detection=False)
result = DeepFace.verify(img1_path, img2_path, model_name=models[5])
plt.imshow(face)
print(result)
face.shape
```
%% Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-302a201e2cc2> in <module>()
4 img1_path = "images/frame_screenshot_07.02.2022_1.png"
5 img2_path = "images/frame_screenshot_07.02.2022.png"
----> 6 face = DeepFace.detectFace(img1_path,(224,224),backends[5], enforce_detection=False)
7 result = DeepFace.verify(img1_path, img2_path, model_name=models[5])
8 plt.imshow(face)
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/deepface/DeepFace.py in detectFace(img_path, target_size, detector_backend, enforce_detection, align)
816
817 img = functions.preprocess_face(img = img_path, target_size = target_size, detector_backend = detector_backend
--> 818 , enforce_detection = enforce_detection, align = align)[0] #preprocess_face returns (1, 224, 224, 3)
819 return img[:, :, ::-1] #bgr to rgb
820
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/deepface/commons/functions.py in preprocess_face(img, target_size, grayscale, enforce_detection, detector_backend, return_region, align)
176 base_img = img.copy()
177
--> 178 img, region = detect_face(img = img, detector_backend = detector_backend, grayscale = grayscale, enforce_detection = enforce_detection, align = align)
179
180 #--------------------------
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/deepface/commons/functions.py in detect_face(img, detector_backend, grayscale, enforce_detection, align)
108 #this call should be completed very fast because it will return found in memory
109 #it will not build face detector model in each call (consider for loops)
--> 110 face_detector = FaceDetector.build_model(detector_backend)
111
112 try:
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/deepface/detectors/FaceDetector.py in build_model(detector_backend)
28 #print(detector_backend," built")
29 else:
---> 30 raise ValueError("invalid detector_backend passed - " + detector_backend)
31
32 return face_detector_obj[detector_backend]
ValueError: invalid detector_backend passed - mediapipe
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
filename = 'video.mp4'
frames_per_second = 24.0
res = '720p'
# Set resolution for the video capture
# Function adapted from https://kirr.co/0l6qmh
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
# Standard Video Dimensions Sizes
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
# grab resolution dimensions and set video capture to it.
def get_dims(cap, res='720'):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
## change the current caputre device
## to the resulting resolution
change_res(cap, width, height)
return width, height
# Video Encoding, might require additional installs
# Types of Codes: http://www.fourcc.org/codecs.php
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
#'mp4': cv2.VideoWriter_fourcc(*'H264'),
'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE['avi']
# FPS = 1/30
# FPS_MS = int(FPS * 1000)
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))
while True:
ret, frame = cap.read()
out.write(frame)
img = resize_img(frame,(220,220))
print(type(img))
print(img.shape)
print(img.size)
obj = RetinaFace.detect_faces(img)
# obj, region = DeepFace.functions.initialize_input(img)
# img = np.asfarray(obj)
# dsize = (int(img.shape[2] * 224), int(img.shape[3] * 224))
# img = cv2.resize(img, (1,(img.shape[2] * 224), (img.shape[2] * 224), )
# print(type(img))
# print(img.shape)
# print(img.size)
# img = DeepFace.functions.preprocess_face(img,(224,224),False,False)
# print(img)
# DeepFace.functions.detect_face(np.asfarray(img_list))
# for key in obj.keys():
# identity = obj[key]
# # print(identity)
# facial_area = identity["facial_area"]
# cv2.rectangle(frame, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)
# # plt.figure(figsize=(20,20))
# plt.imshow(frame[:,:,::-1])
# plt.show
img_cv = cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
```
%% Output
<class 'numpy.ndarray'>
(1, 220, 220, 3)
145200
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-a2440682971e> in <module>()
57 print(img.size)
58
---> 59 obj = RetinaFace.detect_faces(img)
60
61 # obj, region = DeepFace.functions.initialize_input(img)
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/retinaface/RetinaFace.py in detect_faces(img_path, threshold, model, allow_upscaling)
61 """
62
---> 63 img = get_image(img_path)
64
65 #---------------------------
/home/beyondem/miniconda3/envs/compare/lib/python3.6/site-packages/retinaface/RetinaFace.py in get_image(img_path)
52 # Validate image shape
53 if len(img.shape) != 3 or np.prod(img.shape) == 0:
---> 54 raise ValueError("Input image needs to have 3 channels at must not be empty.")
55
56 return img
ValueError: Input image needs to have 3 channels at must not be empty.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment