def post(self): # 文件夹路径 string filepath = os.path.join(app.config['UPLOAD_PATH'], arrow.now().format('YYYYMMDD')) if not os.path.exists(filepath): os.makedirs(filepath) try: # 上传文件命名 随机32位16进制字符 string imgname = '%32x' % random.getrandbits(128) # 文件绝对路径 string imgpath = os.path.join(filepath, '%s.jpg' % imgname) f = request.files['file'] f.save(imgpath) except Exception as e: logger.error(e) return {'message': 'File error'}, 400 # 回调用的消息队列 object que = Queue.Queue() # 识别参数字典 dict r = {'coord': []} app.config['RECGQUE'].put((9, r, que, imgpath)) try: recginfo = que.get(timeout=app.config['TIMEOUT']) except Queue.Empty: return {'message': 'Timeout'}, 408 except Exception as e: logger.error(e) else: return {'coord': r['coord'], 'recginfo': recginfo}, 201
def post(self): parser = reqparse.RequestParser() parser.add_argument('imgurl', type=unicode, required=True, help='A jpg url is require', location='json') parser.add_argument('coord', type=list, required=True, help='A coordinates array is require', location='json') args = parser.parse_args() # 回调用的消息队列 que = Queue.Queue() if app.config['RECGQUE'].qsize() > app.config['MAXSIZE']: return {'message': 'Server Is Busy'}, 449 imgname = '%32x' % random.getrandbits(128) imgpath = os.path.join(app.config['IMG_PATH'], '%s.jpg' % imgname) try: helper.get_url_img(request.json['imgurl'], imgpath) except Exception as e: logger.error('Error url: %s' % request.json['imgurl']) return {'message': 'URL Error'}, 400 app.config['RECGQUE'].put((10, request.json, que, imgpath)) try: recginfo = que.get(timeout=15) os.remove(imgpath) except Queue.Empty: return {'message': 'Timeout'}, 408 except Exception as e: logger.error(e) else: return { 'imgurl': request.json['imgurl'], 'coord': request.json['coord'], 'recginfo': recginfo }, 201
def run(self): while 1: try: if app.config['IS_QUIT']: break p, request, que, imgpath = app.config['RECGQUE'].get(timeout=1) except Queue.Empty: pass except Exception as e: logger.error(e) time.sleep(1) else: try: carinfo = self.cre.imgrecg(imgpath, request['coord']) if carinfo is None: result = None logger.error('Recognise Error') elif carinfo['head']['code'] == 0: result = None else: result = carinfo['body'] except Exception as e: logger.exception(e) result = None try: que.put(result) except Exception as e: logger.error(e)