-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect-wifi.py
executable file
·168 lines (117 loc) · 4.5 KB
/
detect-wifi.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import Image, ImageDraw
import math
from subprocess import call
import time
import datetime
import os
import cv
import sys
from urllib2 import urlopen
REQUIRED_DETECTION=80
THRESHOLD = 130
ALWAYS_SHOW_FRAME = False
RECORD = True
def my_range(start, end, step):
while start<=end:
yield start
start += step
def pix_change1(p1, p2, x, y):
r = 0 #p2[x,y][0]
g = 120 #255
b = 0
return (r, g, b)
def pix_change2(p1, p2, x, y):
r = 0
g = 120
b = 0
return p2[x,y]
def show_frame(frame):
# size = cv.GetSize(frame)
# small = cv.CreateImage((size[0] / 4, size[1] / 4), frame.depth, frame.nChannels)
# cv.Resize(frame, small)
cv.ShowImage(WINDOW_NAME, frame)
frames = []
recording = False
inactive_frames = 0
video_number = 0
CAM_INDEX = 0
if len(sys.argv) > 1:
CAM_INDEX = int(sys.argv[1])
#capture = cv.CaptureFromCAM(CAM_INDEX)
capture = cv.CaptureFromFile("http://192.168.10.195:81/videostream.asf?user=admin&pwd=sdlyr8")
WINDOW_NAME = "Window-%s" % CAM_INDEX
last_frame = cv.QueryFrame(capture)
while True:
if recording:
key = cv.WaitKey(10)
else:
key = cv.WaitKey(500)
if key == 113:
break
frame = cv.QueryFrame(capture)
display_frame = cv.CreateImage(cv.GetSize(frame), 8, 3)
cv.Copy(frame, display_frame)
pixels1 = last_frame
pixels2 = display_frame
diff_count = 0
for y in my_range(0,frame.width-1, 5):
for x in my_range(0, frame.height-1, 5):
red_diff = abs(pixels1[x,y][0] - pixels2[x,y][0])
green_diff = abs(pixels1[x,y][0] - pixels2[x,y][0])
blue_diff = abs(pixels1[x,y][0] - pixels2[x,y][0])
if red_diff + blue_diff + green_diff > THRESHOLD:
diff_count += 1
pixels2[x,y] = pix_change1(pixels1, pixels2, x, y)
if (x >= 2 and y >= 2):
pixels2[x-2 ,y-2] = pix_change1(pixels1, pixels2, x+2, y+2)
pixels2[x ,y-2] = pix_change2(pixels1, pixels2, x, y+2)
pixels2[x-2 ,y] = pix_change2(pixels1, pixels2, x+2, y)
print 'pixel difference: ', diff_count
BAR_WIDTH = 400
color = (0,255,0)
percent = 1.0*diff_count/REQUIRED_DETECTION
if percent >= .6:
color = (0,255,255)
if percent >= 1:
percent=1
color = (0,0,255)
print 'Motion captured'
cv.Rectangle(display_frame, (frame.width/2-BAR_WIDTH/2, frame.height-30), (frame.width/2+BAR_WIDTH/2, frame.height-10), (0,0,0), -1)
cv.Rectangle(display_frame, (frame.width/2-BAR_WIDTH/2, frame.height-30), (frame.width/2+BAR_WIDTH/2, frame.height-10), color)
cv.Rectangle(display_frame, (frame.width/2-BAR_WIDTH/2, frame.height-30), (int(frame.width/2-BAR_WIDTH/2+BAR_WIDTH*percent), frame.height-10), color, -1)
last_frame = cv.CreateImage(cv.GetSize(frame), 8, 3)
cv.Copy(frame, last_frame)
if percent >= 1:
if not recording:
recording = True
video_number += 1
if RECORD:
file_name = "bedroom-%s.avi" % (datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d-%H%M%S"))
writer = cv.CreateVideoWriter(file_name, cv.CV_FOURCC('M', 'J', 'P', 'G'), 5, cv.GetSize(frame), True)
# Save video to Sentry-webapp here
try:
urlopen('http://localhost:4663/api/videos/new/?camera=%s&file_name=%s' % ('Bedroom', file_name))
except:
print 'Could not save new video file'
print cv.NamedWindow(WINDOW_NAME, flags=cv.CV_WINDOW_NORMAL)
cv.MoveWindow(WINDOW_NAME, 20, 20)
show_frame(display_frame)
if RECORD and recording:
cv.WriteFrame(writer, display_frame)
inactive_frames = 0
else:
if recording:
show_frame(display_frame)
inactive_frames += 1
if RECORD:
cv.WriteFrame(writer, display_frame)
if inactive_frames > 20:
recording = False
inactive_frames = 0
if RECORD:
print 'Save video'
del writer
cv.DestroyWindow(WINDOW_NAME)
# cv.ResizeWindow(WINDOW_NAME, 200, 120)
# k=cv.WaitKey(500)
# print k