def get_images(image_id, offset=0, limit=10): log.info('get_images:' + image_id) index_image_api = IndexImages() res = GetImageResponse() try: image_d = rconn.hget(REDIS_INDEXED_SIM_IMAGES_HASH, image_id) if image_d != None: images_dic = pickle.loads(image_d) else: images_dic = index_image_api.get_sim_images(image_id) images = [] for image in images_dic: img = Image() image = img.from_dict(image) images.append(image) rconn.hset(REDIS_INDEXED_SIM_IMAGES_HASH, image_id, pickle.dumps(images_dic)) res.data = images res.message = 'Successful' response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 return res, response_status
def get_image_by_id(image_id): log.info('get_image_by_id') start_time = time.time() index_image_api = IndexImages() res = GetImageResponse() try: image_d = rconn.hget(REDIS_INDEXED_IMAGE_HASH, image_id) if image_d != None: image_dic = pickle.loads(image_d) else: image_dic = index_image_api.get_image(image_id) image_dic['id'] = str(image_dic['_id']) image_dic.pop('_id') img = Image() image = img.from_dict(image_dic) res.data = image res.message = 'Successful' response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 return res, response_status
def get_images(): # noqa: E501 """get_images Returns a list of all available Images # noqa: E501 :rtype: List[Image] """ image_list = [] for image in conn.compute.images(): image_dict = image.to_dict() name = image_dict["name"] id = image_dict["id"] min_ram = image_dict["min_ram"] min_disk = image_dict["min_disk"] updated_at = image_dict["updated_at"] status = image_dict["status"] size = image_dict["size"] image_obj = Image(name=name, id=id, min_disk=min_disk, min_ram=min_ram, status=status, updated_at=updated_at, size=size) image_list.append(image_obj) return image_list
def test_score_image(self): """Test case for score_image Score an image """ image = Image() response = self.client.open('/v2/yolo', method='POST', data=json.dumps(image), content_type='application/json') self.assert200(response, 'Response body is : ' + response.data.decode('utf-8'))
def test_add_image(self): """ Test case for add_image Added a new Image """ body = Image() response = self.client.open('//images', method='POST', data=json.dumps(body), content_type='application/json') self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def get_image_by_host_code_and_product_no(host_code, product_no): log.info('get_image_by_host_code_and_product_no') start_time = time.time() index_image_api = IndexImages() res = GetImageResponse() image = Image() try: api_res = index_image_api.get_image_by_hostcode_and_product_no( host_code, product_no) res.data = image.from_dict(api_res.data.to_dict()) res.message = 'Successful' response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 elapsed_time = time.time() - start_time log.info('time: get_image_by_host_code_and_product_no' + str(elapsed_time)) return res, response_status
def get_objects_by_image_id(image_id): search = Search(log) res = GetObjectsByImageIdResponse() try: image_d = rconn.hget(REDIS_INDEXED_IMAGE_HASH, image_id) if image_d != None: image = pickle.loads(image_d) else: image = search.get_indexed_image(image_id) boxes_array = [] if image is not None: objects = image.get('objects') if objects is not None: for object in objects: object['id'] = str(object['_id']) boxes_array.append(BoxObject.from_dict(object)) rconn.hset(REDIS_INDEXED_IMAGE_HASH, image_id, pickle.dumps(image)) res_data = GetObjectsResponseData() res_data.boxes = boxes_array images = image.get('images') images_list = [] if images is None: res.message = "Successful, but there is no similar images" res_data.images = None else: for image in images: images_list.append(Image().from_dict(image)) res.message = "Successful" res_data.images = images_list res.data = res_data response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 return res, response_status
def feeds(self, offset=0, limit=10): feeds = [] image_ids = [] # x = [randint(1, size) for p in range(1, size)] # for i in x: # image_id = rconn.lindex(REDIS_INDEXED_IMAGE_LIST, i) # image_ids.append(image_id.decode('utf-8')) # images_d = rconn_search_image_0.hmget(REDIS_INDEXED_IMAGE_HASH, image_ids) # for image_d in images_d: # img = Image() # image = pickle.loads(image_d) # image['id'] = str(image['_id']) # i = img.from_dict(image) # feeds.append(i) img = Image() feeds.append(img) return feeds
def add_image(connexion): log.debug('add_image') orm = Images() res = AddImageResponse() data = AddImageResponseData() response_status = 200 if connexion.request.is_json: body = Image.from_dict(connexion.request.get_json()) try: r = orm.images.insert(body.to_dict()) res.message = 'Successful' data.image_id = str(r) log.debug(data.image_id) res.data = data except Exception as e: res.message = str(e) response_status = 400 return res, response_status
def score_image(image=None): # noqa: E501 """Score an image # noqa: E501 :param image: :type image: dict | bytes :rtype: Scores """ if connexion.request.is_json: imageobj = Image.from_dict(connexion.request.get_json()) image = base64.b64decode(imageobj.image) # noqa: E501 imgcv = cv2.imdecode(numpy.asarray(bytearray(image), dtype=numpy.uint8), cv2.IMREAD_COLOR) result = tfnet().return_predict(imgcv) # print(repr(result)) scores = [ScoredClass(voc=r['label'], score=float(r['confidence']), tl_x=float(r['topleft']['x']), tl_y=float(r['topleft']['y']), br_x=float(r['bottomright']['x']), br_y=float(r['bottomright']['y'])) for r in result] return scores
def get_products_by_image_id_and_object_id(image_id, object_id): start_time = time.time() log.debug(image_id) log.debug(object_id) orm = Products() res = GetProductsResponse() response_status = 200 try: r = orm.images.find_one({"_id": ObjectId(image_id)}) res.message = 'Successful' image = Image.from_dict(r) boxes = image.boxes products = boxes[int(object_id)]['products'] res.data = products except Exception as e: res.message = str(e) response_status = 400 elapsed_time = time.time() - start_time log.debug('get_products_by_image_id_and_object_id time: ' + str(elapsed_time)) return res, response_status
def get_images_by_object_id(object_id, offset=0, limit=10): log.info('get_images_by_object_id') search = Search(log) user_api = Users() res = GetImageResponse() log_dic = {} log_dic['device_id'] = 'bluehackmaster' log_dic['object_id'] = object_id rconn.lpush(REDIS_LOG_SEARCH_OBJECT_ID_QUEUE, pickle.dumps(log_dic)) try: object_d = rconn.hget(REDIS_USER_OBJECT_HASH, object_id) if object_d != None: images = pickle.loads(object_d) else: object = user_api.get_object(object_id) images = search.get_images_by_object_vector(object['feature'], limit=limit) images_list = [] if images is None: res.message = "Successful, but there is no similar images" res.data = None response_status = 400 else: for image in images: images_list.append(Image().from_dict(image)) res.message = "Successful" res.data = images_list response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 return res, response_status
def get_objects_by_user_image_file(file): search = Search(log) res = GetObjectsResponse() if file and allowed_file(file.filename): im = PIL.Image.open(file.stream) format = im.format log.debug(im.format) try: if 'gif' == format.lower(): log.debug('gif') im.seek(0) mypalette = im.getpalette() im.putpalette(mypalette) new_im = PIL.Image.new("RGBA", im.size) new_im.paste(im) new_im.save('foo' + '.png') bg = PIL.Image.new("RGB", im.size, (255, 255, 255)) bg.paste(new_im, (0, 0), new_im) bg.save(TMP_IMG, quality=95) im = bg elif 'png' == format.lower(): log.debug('png') bg = PIL.Image.new("RGB", im.size, (255, 255, 255)) bg.paste(im, im) bg.save(TMP_IMG, quality=95) im = bg else: im.save(TMP_IMG) userImage = {} image_url = utils.save_image_to_storage( str(uuid.uuid4()), 'user', TMP_IMG) boxes, objects = search.get_objects(TMP_IMG) images = search.search_image_file(TMP_IMG) user_api = Users() box_dic_list = [] obj_ids = [] i = 0 for box in boxes: b = box.to_dict() b['feature'] = objects[i]['feature'] box_dic_list.append(b) object_id = user_api.add_object('bluehackmaster', b) box.id = object_id obj_ids.append(object_id) rconn.hset(REDIS_USER_OBJECT_HASH, object_id, pickle.dumps(b)) rconn.lpush(REDIS_USER_OBJECT_QUEUE, object_id) i = i + 1 userImage['boxes'] = box_dic_list userImage['url'] = image_url image_id = user_api.add_image('bluehackmaster', userImage) userImage['device_id'] = 'bluehackmaster' userImage['image_id'] = image_id userImage.pop('boxes') userImage['objects'] = obj_ids rconn.lpush(REDIS_LOG_SEARCH_IMAGE_FILE_QUEUE, pickle.dumps(userImage)) res_data = GetObjectsResponseData() res_data.boxes = boxes res_data.image_id = image_id images_list = [] if images is None: res.message = "Successful, but there is no similar images" res_data.images = None else: for image in images: images_list.append(Image().from_dict(image)) res.message = "Successful" res_data.images = images_list res.data = res_data response_status = 200 except Exception as e: log.error(str(e)) response_status = 400 return res, response_status