-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·122 lines (107 loc) · 3.54 KB
/
main.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
import numpy as np
import cv2
import matplotlib.pyplot as plt
import sys
def detectImages(img, classifier):
haarcascade = cv2.CascadeClassifier(classifier)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = haarcascade.detectMultiScale(gray)
for face in faces:
x, y, w, h = face
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 5) #Buat bounding boxes yang masuk di ROI
return img
def gaussianBlur(img, classifier):
haarcascade = cv2.CascadeClassifier(classifier)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = haarcascade.detectMultiScale(gray)
for face in faces:
x, y, w, h = face
img[y:y+h, x:x+w] = cv2.GaussianBlur(img[y:y+h, x:x+w], (41,41), 0) #Blur pixels yang masuk dalam ROI
return img
def mosaicBlur(img, classifier):
haarcascade = cv2.CascadeClassifier(classifier)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = haarcascade.detectMultiScale(gray)
for face in faces:
x, y, w, h = face
steps_count = 8
stepsX = np.linspace(x, x + w, steps_count + 1, dtype=np.int)
stepsY = np.linspace(y, y + h, steps_count + 1, dtype=np.int)
#Iterasi sesuai (steps_count x steps_count) kemudian membuat buat sensor bentuk rectangle dengan warna rata-rata pada tiap regionnya
for directionX in range(steps_count):
for directionY in range(steps_count):
color = cv2.mean(
img[
stepsY[directionY] : stepsY[directionY + 1],
stepsX[directionX] : stepsX[directionX + 1]
]
)
cv2.rectangle(
img,
(stepsX[directionX], stepsY[directionY]),
(stepsX[directionX + 1], stepsY[directionY + 1]),
color,
-1
)
return img
classifier = 'haarcascade_frontalface_default.xml'
img_path = sys.argv[2]
#Features
if sys.argv[1] == '1':
if sys.argv[3] == '1':
img = cv2.imread(img_path)
output1 = detectImages(img, classifier)
plt.imshow(cv2.cvtColor(output1, cv2.COLOR_BGR2RGB))
plt.title('Bounding Box')
plt.grid(None)
plt.xticks([])
plt.yticks([])
plt.show()
elif sys.argv[3] == '2':
img = cv2.imread(img_path)
output2 = gaussianBlur(img, classifier)
plt.imshow(cv2.cvtColor(output2, cv2.COLOR_BGR2RGB))
plt.title('Gaussian Blur')
plt.grid(None)
plt.xticks([])
plt.yticks([])
plt.show()
elif sys.argv[3] == '3':
img = cv2.imread(img_path)
output3 = mosaicBlur(img, classifier)
plt.imshow(cv2.cvtColor(output3, cv2.COLOR_BGR2RGB))
plt.title('Mosaic Blur')
plt.grid(None)
plt.xticks([])
plt.yticks([])
plt.show()
elif sys.argv[3] == '4':
img = cv2.imread(img_path)
img1 = img.copy()
img10 = img.copy()
img11 = img.copy()
output1 = detectImages(img1, classifier)
output2 = gaussianBlur(img10, classifier)
output3 = mosaicBlur(img11, classifier)
output = [img, output1, output2, output3]
titles = ['Original', 'Detected', 'Gaussian Blur', 'Mosaic Blur']
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(cv2.cvtColor(output[i], cv2.COLOR_BGR2RGB))
plt.title(titles[i])
plt.grid(None)
plt.xticks([])
plt.yticks([])
plt.rcParams["figure.figsize"] = (15,10)
plt.show()
else:
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
blurred_img = mosaicBlur(frame, 'haarcascade_frontalface_default.xml')
cv2.imshow('Webcam', frame)
keyboard = cv2.waitKey(30)
if keyboard == 'q' or keyboard == 27:
break
cap.release()
cv2.destroyAllWindows()