-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcarSensor
140 lines (125 loc) · 5.03 KB
/
carSensor
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
'''
Created on Apr 22, 2014
@author: jaredmr
'''
import json
import argparse
import os
import requests
from PIL import Image
import numpy
from time import time
import numpy as np
''' Downloader for a set of files '''
class Downloader:
def __init__(self):
''' initialize the file where the list of URLs is listed, and the
directory where the downloads will be stored'''
self.args = None
self.parse_arguments()
def parse_arguments(self):
''' parse arguments, which include '-i' for input file and
'-d' for download directory'''
parser = argparse.ArgumentParser(prog='Mass downloader', description='A simple script that downloads multiple files from a list of URLs specified in a file', add_help=True)
parser.add_argument('-i', '--input', type=str, action='store', help='Specify the input file containing a list of URLs, default is urls.txt',default='urls.txt')
parser.add_argument('-d', '--dir', type=str, action='store', help='Specify the directory where downloads are stored, default is downloads',default='downloads')
args = parser.parse_args()
self.in_file = args.input
self.dir = args.dir
if not os.path.exists(self.dir):
os.makedirs(self.dir)
def download(self):
''' download the files listed in the input file '''
urls = []
f = open(self.in_file,'r')
for line in f.readlines():
urls.append(line.strip())
f.close()
files = [url.split('/')[-1].strip() for url in urls]
for f,url in zip(files,urls):
filename = self.dir + '/' + f
return self.download_file(url,filename)
def download_file(self,url,filename):
''' download one file given by url and stored in filename'''
#print 'Downloading %s' % url
r = requests.get(url, stream=True)
with open(filename, 'wb') as f:
f.write(r.content)
image = numpy.asarray(Image.open(filename))
image1 = image[850:875,1600:1650,:]
image2 = image[925:950,1600:1650,:]
return [image1,image2]
def increment_db(self):
r= requests.get('http://localhost:3000/api/spots')
jd = json.loads(r.text)
x = jd[0]["numOpenSpots"]# name = jd[0]["name"]
idnum = jd[0]["_id"]
r= requests.delete('http://localhost:3000/api/spots/'+idnum)
r= requests.post('http://localhost:3000/api/spots', data={'name':'adam','numOpenSpots':x+1})
def decrement_db(self):
r= requests.get('http://localhost:3000/api/spots')
jd = json.loads(r.text)
x = jd[0]["numOpenSpots"]# name = jd[0]["name"]
idnum = jd[0]["_id"]
r= requests.delete('http://localhost:3000/api/spots/'+idnum)
r= requests.post('http://localhost:3000/api/spots', data={'name':'adam','numOpenSpots':x-1})
if __name__ == '__main__':
d = Downloader()
prev = time()
cur = prev
images1 = []
images2 = []
for x in xrange(50):
while cur - prev < 1:
cur = time()
prev = cur
temp1, temp2 = d.download()
images1.append(temp1)
images2.append(temp2)
index = 0
# counter = 0
while(True):
median1 = [[[0 for x in xrange(3)] for x in xrange(len(images1[0][0]))] for x in xrange(len(images1[0]))]
median2 = [[[0 for x in xrange(3)] for x in xrange(len(images2[0][0]))] for x in xrange(len(images2[0]))]
for col in xrange(len(images1[0][0])):
for row in xrange(len(images1[0])):
for color in xrange(3):
temp = []
for x in xrange(len(images1)):
temp.append(images1[x][row][col][color])
temp = sorted(temp)[25]
median1[row][col][color] = temp
for col in xrange(len(images2[0][0])):
for row in xrange(len(images2[0])):
for color in xrange(3):
temp = []
for x in xrange(len(images2)):
temp.append(images2[x][row][col][color])
temp = sorted(temp)[25]
median2[row][col][color] = temp
while cur - prev < 1:
cur = time()
temp1, temp2 = d.download()
count = 0
for x in xrange(3):
count = count + np.matrix(abs(temp1 - median1)[x]).sum()
if count > 100000:
# increment in database and break
d.increment_db()
# print 'increment'
break
count = 0
for x in xrange(3):
count = count + np.matrix(abs(temp2 - median2)[x]).sum()
if count > 100000:
# decrement in database and break
d.decrement_db()
# print 'decrement'
break
prev = cur
images1[index] = temp1
images2[index] = temp2
index = (index + 1) % 50
# counter = (counter + 1) % 10
# if not counter:
# print 'still running'