def detect_search_object(image, camera_id): # Detect candidate object => check what object it is data = detector.predict(image) if data['products']: products = search_product(data['products']) bboxes = data['bboxes'] cam = Camera.query.filter(Camera.id == camera_id).first() product_list = [x.product_id for x in cam.products] info = [{ 'id': random_name_generator(), 'bbox': bboxes[i], 'product_id': products[i].id } for i in range(len(products)) if products[i].id in product_list] # track image tracker.update(data['image'], info) # draw object draw_img = tracker.draw() result_image = Image.fromarray(draw_img.astype(np.uint8)) img_byte = io.BytesIO() result_image.save(img_byte, format='jpeg') base64_image = base64.b64encode(img_byte.getvalue()).decode('utf-8') # count object c = count_object(products, product_list) return base64_image, info, c else: img_byte = io.BytesIO() image.save(img_byte, format='jpeg') base64_image = base64.b64encode(img_byte.getvalue()).decode('utf-8') return base64_image, [], []
def handle_out_of_roi(data, room): socketio = SocketIO(app, cors_allowed_origins="*", message_queue='redis://') print('Saving image and send log ...') t = datetime.now().strftime("%Y/%m/%d %H:%M:%S") with app.app_context(): # Log to UI message = '[{}] Object is out of ROI. Saving image...'.format(t) socketio.emit('log', {'log': message}, room=room) log = LogText(message=message, time=t, camera_id=room) db.session.add(log) # Notification camera = Camera.query.filter(Camera.id == room).first() socketio.emit('noti', { 'title': camera.name, 'message': message }, broadcast=True) # Save image to DB send_image = io.BytesIO( base64.b64decode(re.sub("data:image/jpeg;base64", '', data))) image_name = random_name_generator() + '.jpg' firebase_storage.child("images/{}".format(image_name)).put(send_image) url = firebase_storage.child( "images/{}".format(image_name)).get_url(None) image = LogImage(url=url, time=t, camera_id=room) db.session.add(image) db.session.commit()
def save_image_product(data, product_id, product_index): print('Saving image products ...') for i in range(len(data)): image = data[i] image_name = random_name_generator() + '.jpg' send_image = io.BytesIO(base64.b64decode(image)) firebase_storage.child("products/{}/{}".format( product_id, image_name)).put(send_image) url = firebase_storage.child("products/{}/{}".format( product_id, image_name)).get_url(None) image = ProductImage(url=url, product_id=product_id, ann_id=product_index[i]) db.session.add(image) db.session.commit()
def fire_alert(data, room): socketio = SocketIO(app, cors_allowed_origins="*", message_queue='redis://') image_data = base64.b64decode(data) image = Image.open(io.BytesIO(image_data)).convert('RGB') result = fire_alarm.check_fire(image) print('Detecting fire.... Result: {}'.format(result)) with app.app_context(): if result: # Log to UI socketio.emit('fire', {'fire': result}, room=room) t = datetime.now().strftime("%Y/%m/%d %H:%M:%S") message = '[{}] WARNING There may be fire. Please check'.format(t) socketio.emit('log', {'log': message}) # Notification camera = Camera.query.filter(Camera.id == room).first() socketio.emit('noti', { 'title': camera.name, 'message': message }, broadcast=True) # Save log to DB log = LogText(message=message, time=t, camera_id=room) log.save_to_db() send_image = io.BytesIO( base64.b64decode(re.sub("data:image/jpeg;base64", '', data))) image_name = random_name_generator() + '.jpg' firebase_storage.child( "images/{}".format(image_name)).put(send_image) url = firebase_storage.child( "images/{}".format(image_name)).get_url(None) image = LogImage(url=url, time=t, camera_id=room) db.session.add(image) db.session.commit()
def handle_missing_object(data, room): socketio = SocketIO(app, cors_allowed_origins="*", message_queue='redis://') print('Saving image and send log ...') t = datetime.now().strftime("%Y/%m/%d %H:%M:%S") with app.app_context(): # Log to UI logs = [ '{}: {}'.format(c['name'], c['quantity']) for c in data['missing'] ] message = '[{}] WARNING: May be missing: {}. Please check. Saving image...'.format( t, ', '.join(logs)) socketio.emit('log', {'log': message}, room=room) # Notification camera = Camera.query.filter(Camera.id == room).first() socketio.emit('noti', { 'title': camera.name, 'message': message }, broadcast=True) # Logging to db log = LogText(message=message, time=t, camera_id=room) db.session.add(log) # Image send_image = io.BytesIO( base64.b64decode( re.sub("data:image/jpeg;base64", '', data['image']))) image_name = random_name_generator() + '.jpg' firebase_storage.child("images/{}".format(image_name)).put(send_image) url = firebase_storage.child( "images/{}".format(image_name)).get_url(None) image = LogImage(url=url, time=t, camera_id=room) db.session.add(image) db.session.commit()