-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathto_grayscale.py
54 lines (39 loc) · 1.99 KB
/
to_grayscale.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
import cv2
import os
import numpy as np
#This module resizes image from a given directory to 100*100 pixels and writes all images to given directory
count=0
if not os.path.exists('./gray_images'):
os.makedirs('./gray_images')
for path, subdirnames, filenames in os.walk("dataset"):
for dir in subdirnames:
if not os.path.exists(f'./gray_images/{str(dir)}'):
os.makedirs(f'./gray_images/{str(dir)}')
filenames = os.listdir(f'./dataset/{str(dir)}')
print(filenames)
for filename in filenames:
if filename.startswith("."):
print("Skipping File:",filename)#Skipping files that startwith .
continue
print(path,filename)
#img_path=os.path.join(path, filename)#fetching image path
img_path = f'dataset\{str(dir)}\{filename}'
print("img_path",img_path)
facedata = "haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(facedata)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# minisize = (img.shape[1],img.shape[0])
# miniframe = cv2.resize(img, minisize)
faces = cascade.detectMultiScale(gray,1.3,5)
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
sub_face = img[y:y+h, x:x+w]
gray = cv2.cvtColor(sub_face,cv2.COLOR_BGR2GRAY)
equ = cv2.equalizeHist(gray)
final = cv2.medianBlur(equ, 3)
new_path=f"gray_images/{str(dir)}"
print("desired path is",f'./gray_images/{str(dir)}/frame{count}.jpg')#write all images to resizedTrainingImages/id directory
cv2.imwrite(f'./gray_images/{str(dir)}/frame{count}.jpg',final)
count += 1