Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
ParallelPrimeSearch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hailu, Dawit
ParallelPrimeSearch
Commits
f1f2c614
Commit
f1f2c614
authored
3 years ago
by
Hailu, Dawit
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
7b897729
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Retinafacedetection.ipynb
+163
-0
163 additions, 0 deletions
Retinafacedetection.ipynb
with
163 additions
and
0 deletions
Retinafacedetection.ipynb
0 → 100644
+
163
−
0
View file @
f1f2c614
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display, Javascript\n",
"from utilpack.util import *\n",
"\n",
"from retinaface import RetinaFace\n",
"from deepface import DeepFace\n",
"\n",
"from datetime import datetime\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import cv2\n",
"\n",
"import numpy as np\n",
"import os\n",
"import h5py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filename = 'video.avi'\n",
"frames_per_second = 24.0\n",
"res = '720p'\n",
"\n",
"# Set resolution for the video capture\n",
"# Function adapted from https://kirr.co/0l6qmh\n",
"def change_res(cap, width, height):\n",
" cap.set(3, width)\n",
" cap.set(4, height)\n",
"\n",
"# Standard Video Dimensions Sizes\n",
"STD_DIMENSIONS = {\n",
" \"480p\": (640, 480),\n",
" \"720p\": (1280, 720),\n",
" \"1080p\": (1920, 1080),\n",
" \"4k\": (3840, 2160),\n",
"}\n",
"\n",
"\n",
"# grab resolution dimensions and set video capture to it.\n",
"def get_dims(cap, res='1080p'):\n",
" width, height = STD_DIMENSIONS[\"480p\"]\n",
" if res in STD_DIMENSIONS:\n",
" width,height = STD_DIMENSIONS[res]\n",
" ## change the current caputre device\n",
" ## to the resulting resolution\n",
" change_res(cap, width, height)\n",
" return width, height\n",
"\n",
"# Video Encoding, might require additional installs\n",
"# Types of Codes: http://www.fourcc.org/codecs.php\n",
"VIDEO_TYPE = {\n",
" 'avi': cv2.VideoWriter_fourcc(*'XVID'),\n",
" #'mp4': cv2.VideoWriter_fourcc(*'H264'),\n",
" 'mp4': cv2.VideoWriter_fourcc(*'XVID'),\n",
"}\n",
"\n",
"def get_video_type(filename):\n",
" filename, ext = os.path.splitext(filename)\n",
" if ext in VIDEO_TYPE:\n",
" return VIDEO_TYPE[ext]\n",
" return VIDEO_TYPE['avi']\n",
"\n",
"cap = cv2.VideoCapture(0)\n",
"out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))\n",
"start_time = datetime.now()\n",
"\n",
"while True:\n",
" ret, frame = cap.read()\n",
" img_cv = cv2.imshow('frame',frame)\n",
" \n",
" # diff = (datetime.now() - start_time).seconds # converting into seconds\n",
" # print(diff)\n",
" # while( diff <= duration ):\n",
" out.write(frame)\n",
" # rgb_image = cv2.cvtColor(img_cv,PyImageUtil.cv2.COLOR_BGR2RGB).astype(np.float32)\n",
"\n",
" # # cv2.imwrite(\"NewPicture.jpg\",frame)\n",
" # diff = (datetime.now() - start_time).seconds\n",
" # print(diff)\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
" # # img_path = \"NewPicture.jpg\"\n",
" # img = cv2.imread(rgb_image)\n",
" # obj = RetinaFace.detect_faces(rgb_image)\n",
" # RetinaFace.get_image()\n",
" # img.shape\n",
"\n",
" # for key in obj.keys():\n",
" # identity = obj[key]\n",
" # # print(identity)\n",
" # facial_area = identity[\"facial_area\"]\n",
" # cv2.rectangle(img, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)\n",
"\n",
" # plt.figure(figsize=(20,20))\n",
" # plt.imshow(img[:,:,::-1])\n",
" # plt.show\n",
"\n",
"cap.release()\n",
"out.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img_path = \"NewPicture.jpg\"\n",
"img = cv2.imread(img_path)\n",
"obj = RetinaFace.detect_faces(img_path)\n",
"\n",
"img.shape\n",
"\n",
"for key in obj.keys():\n",
" identity = obj[key]\n",
" # print(identity)\n",
" facial_area = identity[\"facial_area\"]\n",
" cv2.rectangle(img, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)\n",
"\n",
"plt.figure(figsize=(20,20))\n",
"plt.imshow(img[:,:,::-1])\n",
"plt.show"
]
}
],
"metadata": {
"interpreter": {
"hash": "e13dd8a849d904006525f725a94fd9290dba9eb122ef777785557ae33676f1f8"
},
"kernelspec": {
"display_name": "Python 3.6.13 ('compare')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
```
python
from
IPython.display
import
display
,
Javascript
from
utilpack.util
import
*
from
retinaface
import
RetinaFace
from
deepface
import
DeepFace
from
datetime
import
datetime
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
import
cv2
import
numpy
as
np
import
os
import
h5py
```
%% Cell type:code id: tags:
```
python
filename
=
'
video.avi
'
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
=
'
1080p
'
):
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
'
]
cap
=
cv2
.
VideoCapture
(
0
)
out
=
cv2
.
VideoWriter
(
filename
,
get_video_type
(
filename
),
25
,
get_dims
(
cap
,
res
))
start_time
=
datetime
.
now
()
while
True
:
ret
,
frame
=
cap
.
read
()
img_cv
=
cv2
.
imshow
(
'
frame
'
,
frame
)
# diff = (datetime.now() - start_time).seconds # converting into seconds
# print(diff)
# while( diff <= duration ):
out
.
write
(
frame
)
# rgb_image = cv2.cvtColor(img_cv,PyImageUtil.cv2.COLOR_BGR2RGB).astype(np.float32)
# # cv2.imwrite("NewPicture.jpg",frame)
# diff = (datetime.now() - start_time).seconds
# print(diff)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'
q
'
):
break
# # img_path = "NewPicture.jpg"
# img = cv2.imread(rgb_image)
# obj = RetinaFace.detect_faces(rgb_image)
# RetinaFace.get_image()
# img.shape
# for key in obj.keys():
# identity = obj[key]
# # print(identity)
# facial_area = identity["facial_area"]
# cv2.rectangle(img, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)
# plt.figure(figsize=(20,20))
# plt.imshow(img[:,:,::-1])
# plt.show
cap
.
release
()
out
.
release
()
cv2
.
destroyAllWindows
()
```
%% Cell type:code id: tags:
```
python
img_path
=
"
NewPicture.jpg
"
img
=
cv2
.
imread
(
img_path
)
obj
=
RetinaFace
.
detect_faces
(
img_path
)
img
.
shape
for
key
in
obj
.
keys
():
identity
=
obj
[
key
]
# print(identity)
facial_area
=
identity
[
"
facial_area
"
]
cv2
.
rectangle
(
img
,
(
facial_area
[
2
],
facial_area
[
3
]),(
facial_area
[
0
],
facial_area
[
1
]),(
255
,
255
,
255
),
1
)
plt
.
figure
(
figsize
=
(
20
,
20
))
plt
.
imshow
(
img
[:,:,::
-
1
])
plt
.
show
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment