-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcropfunc.py
132 lines (88 loc) · 4.54 KB
/
cropfunc.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 24 22:53:44 2018
@author: Shantam Vijayputra
"""
#importng the libraries
import pandas as pd
import numpy as np
import cv2
import os
#creating classmethod
class Imagescale(object):
#calling classmethod and defining image resize functio
@classmethod
def resizeimage(self):
#changing the directory tho extracted dataset of wiki images
os.chdir("c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations/images/")
#list of all folders tha contain images
dir = os.listdir()
#looping through all the directories and images
for i in dir:
#listing all
list = os.listdir()
#for iamge n each dir
for j in list:
#reading the image
img = cv2.imread(j)
#resizing the image
rm = cv2.resize(img,(64,64))
#saving the resized image
cv2.imwrite('C:/Users/Shantam Vijayputra/Desktop/images/'+str(j),rm)
#returning to the main dataset folder
return os.chdir('c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations')
#calling classmethod and efinig the the crop function
@classmethod
def detectfaces_crop(self):
#the directory location where all the image are scalled and resized
os.chdir("c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations/images")
#list of all the images
images = os.listdir()
#detection of faces in the images
#location of haarcascade dataset
face_cascade=cv2.CascadeClassifier('C://Users/Haarcascades_Datasets/haarcascade_frontalface_default.xml')
for image in images:
#reading the image
img = cv2.imread(image)
#conversion of grayscale
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #will display another window with same vedio feed but grey in color .You can always use different option and for that refer the official Documentations.
#detection of face coordinates
faces=face_cascade.detectMultiScale(gray,1.3,3)
#detection of eye and creating the rectangle box around faces and eye
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),5)
#croping the image contains only face
roi_gray = gray[x:x+w,y:y+h]
roi_color = img[x:x+w,y:y+h]
crop_img = img[y:y+h, x:x+w]
#saving the croped face image
cv2.imwrite('C:/Users/Shantam Vijayputra/Desktop/Python Project and Implemetations/cropedimages/'+str(image),crop_img)
#returning to the main directory
return os.chdir('c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations')
#calling the classmethod and defing function that convert image to gray
@classmethod
def clor2gray(self):
#location of the images
os.chdir('c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations/cropedimages/')
#list of croped images
crops = os.listdir()
#looping through all the images
for crop in crops :
#reading the image
img = cv2.imread(crop)
#conversion of the image
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#saving the converted image
cv2.imwrite('C:/Users/Shantam Vijayputra/Desktop/Python Projects and Implementations/greyed/'+str(crop),img)
#returning to the main director
return os.chdir('c://Users/Shantam Vijayputra/Desktop/Python Projects and Implementations')
#calling the main method
if __name__ == "__main__":
#printing doc
print(__doc__)
#object creation
obj = Imagescale()
#calling the object functionalities.
obj.resizeimage()
obj.detectfaces_crop()
obj.color2gray()