-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_creating.py
More file actions
107 lines (90 loc) · 3.4 KB
/
data_creating.py
File metadata and controls
107 lines (90 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
"""data_creating.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1EfMZWOKBd-xZV_XtJ22sd9yGrMHGgMPd
"""
# Commented out IPython magic to ensure Python compatibility.
# only for video to photo converter
# 1 seconds = 1000 milliseconds.
import cv2
vidcap = cv2.VideoCapture('/content/gdrive/MyDrive/jyjyj/VID20210314210243.mp4')
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000) # just cue to 1 sec. position
hasFrames,image = vidcap.read()
if hasFrames:
cv2.imwrite("/content/gdrive/MyDrive/jj/"+str(count)+".jpg", image) # save frame as JPG file
return hasFrames
sec = 0
frameRate = 0.02 #//it will capture image in each 0.5 second
count=1
success = getFrame(sec)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)
# if the photoes are big then this will be used to work on one image and adjust proper for CNN
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image
# %matplotlib inline
path='/content/gdrive/MyDrive/jj/' #the path of images
path_resize='/content/gdrive/MyDrive/jyjyj/' #path of resized images
testim = Image.open(path+"1.jpg", "r")
testim_rotate = testim.transpose(Image.ROTATE_90)
testim_resize = testim_rotate.resize((130, 175), Image.BOX)
imshow(np.asarray(testim_resize))
testim_resize.save(path_resize+"image_1.jpg")
#after getting proper image for CNN then it code to do for the rest of images
import os
import numpy as np
from PIL import Image
#read paths
source_path="/content/gdrive/MyDrive/jj/" #the path of images
dest_path="/content/gdrive/MyDrive/kk/" #path of resized images
# declare count
count=0
# read all images from source path
for count,image_file in enumerate(os.listdir(source_path)):
image_path = os.path.join(source_path, image_file)
# resizing of image
testim = Image.open(image_path, "r")
testim_rotate = testim.transpose(Image.ROTATE_90)
testim_resize = testim_rotate.resize((130, 175), Image.BOX)
testim_resize.save(dest_path+str(count)+".jpg")
# the program will extract the faces from the images /// for only one folder
import cv2
import sys
import os
import numpy as np
from PIL import Image as pil_image
from google.colab.patches import cv2_imshow
count=0
harcascadePath = '/content/gdrive/MyDrive/face_recognition/haarcascade_frontalface_default.xml'
detector = cv2.CascadeClassifier(harcascadePath)
th = '/content/gdrive/MyDrive/face_recognition/Faces/bros/Vrushank/'
imagePaths = [os.path.join(th,f) for f in os.listdir(th)]
new = '/content/gdrive/MyDrive/dd/'
face_arrr = []
label_arrr = []
try:
for path in imagePaths:
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
if faces is not ():
for (x,y,w,h) in faces:
#cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1,1)
face = image[y-50:y+h+50,x-50:x+w+50]
count += 1
# cv2.imwrite(new+str(count)+ 'face' + ".jpg",face)
face_arrr.append(face)
label_arrr.append(faces)
except:
print("Oops!", sys.exc_info()[0], "occured.")
print("done")
# cv2_imshow(face)
# # cv2.waitkey(0)
# print(len(face))
# print(face.shape)