def snapshot(): cam = Camera() photo = cam.get_frame() file = open('test.jpg', 'wb+') file.write(photo) file.close() return render_template('index.html')
def gen(Camera): """Video streaming generator function.""" while True: frame = Camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') color_image = cv2.imdecode(np.fromstring(frame, dtype=np.uint8), cv2.IMREAD_COLOR) roi = color_image[120:240, :] image_array = roi.reshape(1, 115200).astype(np.float32)
class CountThread(Thread): """Stream data on thread""" def __init__(self): self.delay = 1 / 30 super(CountThread, self).__init__() self.cam = Camera() def get_data(self): """ Get data and emit to socket """ while True: image = self.cam.get_frame() socketio.emit('imageSend', {'buffer': image}, namespace="/client") sleep(self.delay) def run(self): """Default run method""" self.get_data()
def path_finding(): global map, last_card_read, motor, heading, card_detected dist_rec = [] direc_rec = [] camera = Camera('auto') node = map.find_node(map.path['header']['start']) while node.next_node_id: dist_rec.append(node.get_edge_len(node.next_node_id)) if node.prev_node_id: direc_rec.append(node.get_direction()) print(node.id) node = map.find_node(map.path[node.id]['next']) update_heading() current_node_idx = 0 while True: frame, cmd = camera.get_frame() if mode == 'auto' and card_detected: print('card detected') if current_node_idx == len(direc_rec): print('complete') return motor.stop() time.sleep(0.1) update_heading() goal_ang = heading + direc_rec[current_node_idx] if goal_ang > 360: goal_ang -= 360 elif goal_ang < 0: goal_ang += 360 print('goal ang:', goal_ang) heading = goal_ang card_detected = False current_node_idx += 1 cmd = 'l' last_card_read = datetime.now() elif mode == 'auto' or mode == 'debug': cmd = convert_cmd(cmd) if mode == 'auto': cmd_to_motor(cmd)
def gen(Camera): """Video streaming generator function.""" while True: frame = Camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def index(cmd=None): """Video streaming home page.""" result = '' camera = Camera() if cmd == 'image': frame = camera.get_frame() conn = http.client.HTTPSConnection( 'eastasia.api.cognitive.microsoft.com') conn.request('POST', "/vision/v1.0/analyze?%s" % analyze_params, frame, cv_headers) response = conn.getresponse() data = response.read() conn.close() dec_data = json.loads(data.decode('utf-8')) result_list = [] caption = dec_data['description']['captions'][0]['text'] result_list.append(caption) categories = dec_data['categories'] if 'categories' in dec_data else [] c_detail = {} l_detail = {} for cat in categories: if cat['name'] == 'people_': c_detail = cat['detail'] if 'detail' in cat else {} elif cat['name'] == 'outdoor_' or cat['name'] == 'building_': l_detail = cat['detail'] if 'detail' in cat else {} if c_detail: celebrities = [] for cel in c_detail['celebrities']: celebrities.append(cel['name']) if celebrities: result_list.append(' '.join(celebrities)) elif l_detail: landmarks = [] for lan in l_detail['landmarks']: landmarks.append(lan['name']) if landmarks: result_list.append(' '.join(landmarks)) # result = "{}".format(dec_data['description']['captions'][0]['text']) result = '\n'.join(result_list) elif cmd == 'word': frame = camera.get_frame() conn = http.client.HTTPSConnection( 'eastasia.api.cognitive.microsoft.com') conn.request('POST', "/vision/v1.0/ocr?%s" % ocr_params, frame, cv_headers) response = conn.getresponse() data = response.read() conn.close() dec_data = json.loads(data.decode('utf-8')) words_list = [] for big_box in dec_data['regions']: for small_box in big_box['lines']: tmp = [] for words in small_box['words']: tmp.append(words['text']) words_list.append(' '.join(tmp)) result = '\n'.join(words_list) if len( words_list) != 0 else 'There are no words in the image.' tl_params = urllib.parse.urlencode({ # Request parameters 'text': result, 'to': 'zh', }) conn = http.client.HTTPConnection('api.microsofttranslator.com') conn.request('GET', "/V2/Http.svc/Translate?%s" % tl_params, headers=tl_headers) response = conn.getresponse() tl_data = response.read() conn.close() tl_data = tl_data.replace( b'<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', b'') tl_data = tl_data.replace(b'</string>', b'') dec_tl_data = tl_data.decode('utf-8') result = dec_tl_data return render_template('index.html', result=result)
sio = socketio.Client(logger=True, engineio_logger=True) @sio.event def connect(): print("I'm connected!") sio.emit('aaa') @sio.event def connect_error(data): print("The connection failed!") exit() @sio.event def disconnect(): print("I'm disconnected!") exit() sio.connect('http://192.168.0.10:8000', namespaces='/test') camera = Camera() while True: print("Frame") frame = camera.get_frame() sio.emit('frame', frame, '/test')