-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_detection_using_sift_and_android_as_cam_via_ipwebcam.py
97 lines (68 loc) · 3.25 KB
/
object_detection_using_sift_and_android_as_cam_via_ipwebcam.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
import cv2
import urllib.request
import numpy as np
def sift_detector(new_image, image_template):
# Function that compares input image to template
# It then returns the number of SIFT matches between them
image1 = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)
image2 = image_template
# Create SIFT detector object
sift = cv2.xfeatures2d.SIFT_create()
# Obtain the keypoints and descriptors using SIFT
keypoints_1, descriptors_1 = sift.detectAndCompute(image1, None)
keypoints_2, descriptors_2 = sift.detectAndCompute(image2, None)
# Define parameters for our Flann Matcher
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 3)
search_params = dict(checks = 100)
# Create the Flann Matcher object
flann = cv2.FlannBasedMatcher(index_params, search_params)
# Obtain matches using K-Nearest Neighbor Method
# the result 'matchs' is the number of similar matches found in both images
matches = flann.knnMatch(descriptors_1, descriptors_2, k=2)
# Store good matches using Lowe's ratio test
good_matches = []
for m,n in matches:
if m.distance < 0.7 * n.distance:
good_matches.append(m)
return len(good_matches)
stream = urllib.request.urlopen('http://192.168.43.1:8080/video') #here use your server ip/video
bytes = bytes()
image_template = cv2.imread('mob.jpeg', 0) #template image
while True:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
# Get height and width of webcam frame
height, width = frame.shape[:2]
# Define ROI Box Dimensions
top_left_x = width // 3
top_left_y = (height // 2) + (height // 4)
bottom_right_x = (width // 3) * 2
bottom_right_y = (height // 2) - (height // 4)
# Draw rectangular window for our region of interest
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)
# Crop window of observation we defined above
cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]
# Flip frame orientation horizontally
frame = cv2.flip(frame,1)
# Get number of SIFT matches
matches = sift_detector(cropped, image_template)
# Display status string showing the current no. of matches
cv2.putText(frame,str(matches),(450,450), cv2.FONT_HERSHEY_COMPLEX, 2,(0,255,0),1)
# Our threshold to indicate object deteciton
# We use 10 since the SIFT detector returns little false positves
threshold = 10
# If matches exceed our threshold then object has been detected
if matches > threshold:
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), (0,255,0), 3)
cv2.putText(frame,'Object Found',(50,50), cv2.FONT_HERSHEY_COMPLEX, 2 ,(0,255,0), 2)
cv2.imshow('Object Detector using SIFT', frame)
if cv2.waitKey(1) == 13: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()