Skip to content

Commit fa2d5a2

Browse files
adding more example and fixing previous codes
1 parent ceec6c2 commit fa2d5a2

24 files changed

+192
-44
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ env/
4141
venv/
4242
ENV/
4343
env.bak/
44-
venv.bak/
44+
venv.bak/
45+
46+
# others
47+
test.py

03. Edge Detection/Real Time Edge Detection.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44

55
cap = cv2.VideoCapture(0)
66

7-
while(1):
7+
while True:
88

9-
_, frame = cap.read()
9+
check, frame = cap.read()
10+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
1011

11-
cv2.imshow('Original',frame)
12-
edges = cv2.Canny(frame,100,200)
13-
cv2.imshow('Edges',edges)
12+
if check:
13+
# applying canny edge transformation
14+
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
1415

15-
k = cv2.waitKey(5) & 0xFF
16-
if k == 27:
17-
break
16+
# showing the output frame
17+
cv2.imshow('Original',frame)
18+
cv2.imshow('Edges', edges)
1819

20+
if cv2.waitKey(5) & 0xFF == 27:
21+
break
22+
23+
cap.release()
1924
cv2.destroyAllWindows()
20-
cap.release()
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
'''
22
Edge detection with canny algo
3-
edges = cv2.Canny('/path/to/img', minVal, maxVal, apertureSize, L2gradient)
3+
edges = cv2.Canny('/path/to/img', threshold1, threshold2, apertureSize, L2gradient)
44
5-
minVal= minimu, intensity gradent
6-
L2gradient: Its default value is false, if value is true, Canny () uses a more computationally expensive equation to detect edges,
5+
L2gradient: Its default value is false, if value is true, Canny () uses a more computationally expensive equation to detect edges,
76
which provides more accuracy at the cost of resources.
87
'''
98
import cv2
109

1110
img = cv2.imread(r'./Media/face-001.jpg')
12-
edges = cv2.Canny(img, 100, 100)
13-
print(edges)
11+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
1412

15-
cv2.imshow("Edge Detected Image",edges)
13+
# applying canny edge transformations
14+
edges = cv2.Canny(gray_img, threshold1=30, threshold2=100)
15+
16+
# showing the output frame
17+
cv2.imshow("Edge Detected Image", edges)
1618

1719
k = cv2.waitKey(0) & 0xFF
1820

1921
if k == 27:
2022
cv2.destroyAllWindows()
2123
elif k == ord('s'):
2224
cv2.imwrite("./Media/edge-detection.jpg", edges)
23-
cv2.destroyAllWindows()
25+
cv2.destroyAllWindows()

07. Face Detection/blurTheFace.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
''' Real time Face bluring using webcam '''
22

3-
import numpy as np
43
import cv2
4+
import numpy as np
5+
import time
56

6-
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
7+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
8+
prototxt_path = "07. Face Detection/deploy.prototxt.txt"
9+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
10+
model_path = "07. Face Detection/res10_300x300_ssd_iter_140000.caffemodel"
711

8-
cap = cv2.VideoCapture(0)
12+
# load Caffe model
13+
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
914

10-
while 1:
11-
ret, frame = cap.read()
12-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
13-
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
14-
15-
for (x,y,w,h) in faces:
16-
roi_color = frame[y:y+h, x:x+w]
17-
roi_color = cv2.GaussianBlur(roi_color, (23, 23), 30)
18-
frame[y: y+h, x: x+w] = roi_color
15+
cap = cv2.VideoCapture(0)
1916

20-
cv2.imshow('bluredFace', frame)
21-
if cv2.waitKey(30) & 0xff == ord("q"):
17+
while True:
18+
start = time.time()
19+
_, image = cap.read()
20+
# get width and height of the image
21+
h, w = image.shape[:2]
22+
kernel_width = (w // 7) | 1
23+
kernel_height = (h // 7) | 1
24+
# preprocess the image: resize and performs mean subtraction
25+
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
26+
# set the image into the input of the neural network
27+
model.setInput(blob)
28+
# perform inference and get the result
29+
output = np.squeeze(model.forward())
30+
for i in range(0, output.shape[0]):
31+
confidence = output[i, 2]
32+
# get the confidence
33+
# if confidence is above 40%, then blur the bounding box (face)
34+
if confidence > 0.4:
35+
# get the surrounding box cordinates and upscale them to original image
36+
box = output[i, 3:7] * np.array([w, h, w, h])
37+
# convert to integers
38+
start_x, start_y, end_x, end_y = box.astype(np.int)
39+
# get the face image
40+
face = image[start_y: end_y, start_x: end_x]
41+
# apply gaussian blur to this face
42+
face = cv2.GaussianBlur(face, (kernel_width, kernel_height), 0)
43+
# put the blurred face into the original image
44+
image[start_y: end_y, start_x: end_x] = face
45+
cv2.imshow("image", image)
46+
if cv2.waitKey(1) == ord("q"):
2247
break
23-
48+
time_elapsed = time.time() - start
49+
fps = 1 / time_elapsed
50+
print("FPS:", fps)
2451

25-
cap.release()
26-
cv2.destroyAllWindows()
52+
cv2.destroyAllWindows()
53+
cap.release()

07. Face Detection/realTimeFaceDetectionDNN.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import numpy as np
44
import cv2
55

6-
proto='./deploy.prototxt.txt'
7-
model='./res10_300x300_ssd_iter_140000.caffemodel'
8-
confThresh=0.5
9-
net = cv2.dnn.readNetFromCaffe(proto, model)
6+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
7+
prototxt_path = "07. Face Detection/deploy.prototxt.txt"
8+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
9+
model_path = "07. Face Detection/res10_300x300_ssd_iter_140000.caffemodel"
10+
11+
confThresh = 0.5
12+
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
1013

1114
cam=cv2.VideoCapture(0)
15+
1216
while True:
13-
ret,frame=cam.read()
14-
frame=cv2.flip(frame,1)
17+
ret, frame = cam.read()
18+
frame = cv2.flip(frame, 1)
1519
(h, w) = frame.shape[:2]
16-
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,(300, 300), (104.0, 177.0, 123.0))
20+
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
1721

1822
net.setInput(blob)
1923
detections = net.forward()

17. Color Trackbar/colorTrackbar.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import numpy as np
44
import cv2
5+
56
def nothing(x):
67
pass
78

89
# Create a black image, a window
9-
img = np.zeros((300,512,3), np.uint8)
10-
cv2.namedWindow('image')
10+
img = np.zeros((300, 512, 3), np.uint8)
11+
cv2.namedWindow('Color Tracker')
1112

1213
# create trackbars for color change
1314
cv2.createTrackbar('R','image',0,255,nothing)
@@ -35,4 +36,4 @@ def nothing(x):
3536
else:
3637
img[:] = [b,g,r]
3738

38-
cv2.destroyAllWindows()
39+
cv2.destroyAllWindows()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import cv2
3+
4+
# loading and resizing the image
5+
img1 = cv2.imread('./Media/book.png')
6+
img2 = cv2.imread('./Media/book_on_table.jpeg')
7+
8+
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
9+
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
10+
11+
sift = cv2.SIFT_create()
12+
13+
keypoints1, descrptors1 = sift.detectAndCompute(img1, None)
14+
keypoints2, descrptors2 = sift.detectAndCompute(img2, None)
15+
16+
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)
17+
18+
matches = bf.match(descrptors1, descrptors2)
19+
matches = sorted(matches, key= lambda x: x.distance)
20+
21+
matched_img = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:20], img2, flags=2)
22+
23+
# showing the output
24+
cv2.imshow('image', cv2.resize(matched_img, (800, 600)))
25+
26+
k = cv2.waitKey(0) & 0xff
27+
28+
if k == 27:
29+
cv2.destroyAllWindows()

18. SIFT Feature Extraction/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
**SIFT** stands for `Scale Invariant Feature Transform`, it is a feature extraction method (among others, such as `HOG feature extraction`) where image content is transformed into local feature coordinates that are invariant to translation, scale and other image transformations.
2+
3+
Below are the advantages of SIFT:
4+
5+
- Locality: Features are local; robust to occlusion and clutter.
6+
- Distinctiveness: Individual features extracted can be matched to a large dataset of objects.
7+
- Quantity: Using SIFT, we can extract many features from small objects.
8+
- Efficiency: SIFT is close to real-time performance.
9+
10+
[SIFT original paper](https://www.cs.ubc.ca/~lowe/papers/ijcv04.pdf)

18. SIFT Feature Extraction/basic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
import cv2
3+
4+
img = cv2.imread('./Media/apple.jpeg')
5+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
6+
7+
sift = cv2.SIFT_create()
8+
9+
keypoints, descrptors = sift.detectAndCompute(img, None)
10+
sift_image = cv2.drawKeypoints(gray_img, keypoints, img)
11+
12+
cv2.imshow('image', sift_image)
13+
14+
k = cv2.waitKey(0) & 0xff
15+
16+
if k == 27:
17+
cv2.destroyAllWindows()

19. Hog Feature Extraction/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)