def detection_plate(data_dir): ''' Detect images in data dir. Args: img_array -- The numpy array of image to be predicted. ''' try: from hyperlpr import HyperLPR_PlateRecogntion print(os.path.split(os.path.realpath(__file__))[0]) print('Start') print('dir:', data_dir) result_list = [] for img_name in os.listdir(data_dir): print('img name:', img_name) if (img_name.lower().find('.png') < 0 and img_name.lower().find('.jpeg') < 0 and img_name.lower().find('.jpg') < 0 and img_name.lower().find('.bmp') < 0): continue img_path = os.path.join(data_dir, img_name) print('Start read img') image = cv2.imread(img_path) print('Start lpr detection') result = HyperLPR_PlateRecogntion( image, path=os.path.join( os.path.split(os.path.realpath(__file__))[0], 'hypercar')) result_list.append(result) except Exception as e: print(e)
def detection_plate_one_img(img_array): ''' Detect one image. Args: img_array -- The numpy array of image to be predicted. ''' try: from hyperlpr import HyperLPR_PlateRecogntion result_list = HyperLPR_PlateRecogntion( img_array, path=os.path.join( os.path.split(os.path.realpath(__file__))[0], 'hypercar')) return result_list except Exception as e: print(e)
def lpr(file_name): body = app.current_request.raw_body # parsed = parse_qs(request.raw_body, encoding='utf-8') temp_file = os.getcwd() + file_name with open(temp_file, 'wb') as f: f.write(body) # 使用Opencv转换一下图片格式和名称 img = cv2.imread(temp_file) # 识别结果 data = HyperLPR_PlateRecogntion(img) if data: for i in data: if i[1] > 0.8: return Response({'lp': i[0]}) else: return Response('error happens')
def post(self): """识别车牌 """ body = self.get_body_json() url = body.pop("url") image = io.imread(url) info = HyperLPR_PlateRecogntion(image) if info: item = info[0] logging.info("识别车牌:%s", info) self.success(data={ "lp": item[0], "precision": item[1], "pos": item[2] }) return logging.info("识别失败:%s", url) self.success(status="unknown", message="车牌识别失败")
def upload_to_s3(file_name): # get raw body of PUT request body = app.current_request.raw_body # write body to tmp file tmp_file_name = os.getcwd() + file_name with open(tmp_file_name, 'wb') as tmp_file: tmp_file.write(body) # 使用Opencv转换一下图片格式和名称 img = cv2.imread(tmp_file_name) # 识别结果 data = HyperLPR_PlateRecogntion(img) if data: for i in data: if i[1] > 0.8: os.remove(tmp_file_name) return Response(body='lpr: {}'.format(i[0]), status_code=200, headers={'Content-Type': 'text/plain'}) else: return Response(body='error: {}'.format('error happens'), status_code=400, headers={'Content-Type': 'text/plain'})
import cv2 import os from hyperlpr import HyperLPR_PlateRecogntion img_dir = "/home/ubuntu/Desktop/2019 Semester 1/Project/DemoHyperLPR/img/" for filename in os.listdir(img_dir): print("Working on image: "+filename) if '.jpg' in filename: img = cv2.imread(img_dir + filename) recognitionResult = HyperLPR_PlateRecogntion(img) maxConfidence = 0 maxRow = 0 imageName = 'default' # identify the result with the highest confidence for i in recognitionResult: if i[1] > maxConfidence: maxConfidence = i[1] maxRow = i if maxConfidence > 0.8: imageName = maxRow[0] os.rename(img_dir+filename, img_dir+imageName+'.jpg')
return image_code def base64_to_image(base64_code): img_data = base64.b64decode(base64_code) img_array = np.fromstring(img_data, np.uint8) img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR) return img aa=image_to_base64(image) data={} data['image']=aa bb=base64_to_image(data['image']) #print(HyperLPR_PlateRecogntion(bb)[0][0],filename[0].strip('.jpg')) temp=[bb.shape] if(len(HyperLPR_PlateRecogntion(bb))==0): shutil.copyfile(path,"/Users/reocar/Documents/interface/lost_pic/"+filename[i]) output_[filename[i].strip('.jpg')]=temp continue if(len(HyperLPR_PlateRecogntion(bb)[0][0])!=7): temp.append(HyperLPR_PlateRecogntion(bb)[0][0][0:7]) else: temp.append(HyperLPR_PlateRecogntion(bb)[0][0]) output[filename[i].strip('.jpg')]=temp
def recognize(): """ 处理上传文件并识别车牌号码 :return: dict """ if not check_uploaded_file1('photo', request.files): return make_api_response(2, '未找到匹配的图片') photo = request.files.get('photo') if not check_uploaded_file2(photo.filename): return make_api_response(2, '未接收到任何图片,请重新上传图片') if not (photo and allowed_file(photo.filename)): return make_api_response(2, '上传的文件格式无效。') relative_path = save_photo(photo) photo_path = Path(relative_path) if not photo_path.is_file(): return make_api_response(2, '图片文件上传失败') if not check_uploaded_file3(relative_path): return make_api_response(2, '图片大小为0,请重新上传图片') image = Image.open(relative_path) img_dpi = "%dx%d" % (image.size[0], image.size[1]) img_format = image.format img_size = "%dKB" % (os.path.getsize(relative_path) / 1024) start = time.clock() # 识别车牌号码 image = cv2.imread(relative_path) if image is None: return make_api_response(status=1, msg='OpenCV读取图片失败', result_photo=photo_path.name, img_dpi=img_dpi, img_format=img_format, img_size=img_size) result_list = HyperLPR_PlateRecogntion(image) elapsed = "%.2fs" % (time.clock() - start) if not (result_list and len(result_list) == 1) or not (result_list[0] and len(result_list[0]) == 3): return make_api_response(status=1, msg='操作成功,但未找到有效车牌号', result_photo=photo_path.name, img_dpi=img_dpi, img_format=img_format, img_size=img_size, used_time=elapsed) plate = result_list[0][0] confidence = result_list[0][1] location = result_list[0][2] top_left_x = location[0] top_left_y = location[1] top_right_x = location[2] top_right_y = location[1] bottom_right_x = location[2] bottom_right_y = location[3] bottom_left_x = location[0] bottom_left_y = location[3] rectangle_point_locations = [(top_left_x, top_left_y), (top_right_x, top_right_y), (bottom_right_x, bottom_right_y), (bottom_left_x, bottom_left_y)] result_photo = mark_photo(relative_path, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y)) return make_api_response(status=0, msg='车牌号码识别成功......', plate=plate, confidence=float(confidence), result_photo=Path(result_photo).name, location=rectangle_point_locations, img_dpi=img_dpi, img_format=img_format, img_size=img_size, used_time=elapsed)