中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

漣源網(wǎng)站建設(shè)網(wǎng)絡(luò)推廣加盟

漣源網(wǎng)站建設(shè),網(wǎng)絡(luò)推廣加盟,關(guān)于app的策劃書,長沙seo推廣今天優(yōu)化了下之前的初步識(shí)別服務(wù)的python代碼和html代碼。 采用flask paddleocr bootstrap快速搭建OCR識(shí)別服務(wù)。 代碼結(jié)構(gòu)如下&#xff1a; 模板頁面代碼文件如下&#xff1a; upload.html : <!DOCTYPE html> <html> <meta charset"utf-8"> …

今天優(yōu)化了下之前的初步識(shí)別服務(wù)的python代碼和html代碼。

采用flask + paddleocr+ bootstrap快速搭建OCR識(shí)別服務(wù)。

代碼結(jié)構(gòu)如下:

模板頁面代碼文件如下:

upload.html :

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head><title>PandaCodeOCR</title><!--靜態(tài)加載 樣式--><link rel="stylesheet" href={{ url_for('static',filename='bootstrap3/css/bootstrap.min.css') }}></link><style>body {font-family: Arial, sans-serif;margin: 0;padding: 0;}.header {background-color: #f0f0f0;text-align: center;padding: 20px;}.title {font-size: 32px;margin-bottom: 10px;}.menu {list-style-type: none;margin: 0;padding: 0;overflow: hidden;background-color: #FFDEAD;border: 2px solid #DCDCDC;}.menu li {float: left;font-size: 24px;}.menu li a {display: block;color: #333;text-align: center;padding: 14px 16px;text-decoration: none;}.menu li a:hover {background-color: #ddd;}.content {padding: 20px;border: 2px solid blue;}</style>
</head>
<body>
<div class="header"><div class="title">PandaCodeOCR</div>
</div><ul class="menu"><li><a href="/upload/">通用文本識(shí)別</a></li>
</ul><div class="content"><!--上傳圖片文件--><div id="upload_file"><form id="fileForm" action="/upload/" method="POST" enctype="multipart/form-data"><div class="form-group"><input type="file" class="form-control" id="upload_file" name="upload_file"><label class="sr-only" for="upload_file">上傳圖片</label></div></form></div>
</div>
</div><div id="show" style="display: none;"><!--顯示上傳的圖片--><div class="col-md-6" style="border: 2px solid #ddd;"><span class="label label-info">上傳圖片</span><!--靜態(tài)加載 圖片, url_for() 動(dòng)態(tài)生成路徑 --><img src="" alt="Image preview area..." title="preview-img" class="img-responsive"></div><div class="col-md-6" style="border: 2px solid #ddd;"><!--顯示識(shí)別結(jié)果JSON報(bào)文列表--><span class="label label-info">識(shí)別結(jié)果:</span><!-- 結(jié)果顯示區(qū) --><div id="result_show">加載中......</div></div>
</div>
</body>
</html>
<!--靜態(tài)加載 script-->
<script src={{ url_for('static',filename='jquery1.3.3/jquery.min.js') }}></script>
<script src={{ url_for('static',filename='js/jquery-form.js') }}></script>
<script type="text/javascript">var fileInput = document.querySelector('input[type=file]');var previewImg = document.querySelector('img');{#上傳圖片事件#}fileInput.addEventListener('change', function () {var file = this.files[0];var reader = new FileReader();//顯示預(yù)覽界面$("#show").css("display", "block");// 監(jiān)聽reader對象的的onload事件,當(dāng)圖片加載完成時(shí),把base64編碼賦值給預(yù)覽圖片reader.addEventListener("load", function () {previewImg.src = reader.result;}, false);// 調(diào)用reader.readAsDataURL()方法,把圖片轉(zhuǎn)成base64reader.readAsDataURL(file);//初始化輸出結(jié)果信息$("#result_show").html("加載中......");{#上傳圖片識(shí)別表單事件,并顯示識(shí)別結(jié)果信息#}{# ajaxSubmit 請求異步響應(yīng)#}$("#fileForm").ajaxSubmit(function (data) {var inner = "";//alert(data['recognize_time'])//循環(huán)輸出返回結(jié)果,響應(yīng)識(shí)別結(jié)果為每行列表for (var i in data['result']) {var value = data['result'][i]['text'];inner += "<p class='text-left'>" + value + "</p>";}//清空輸出結(jié)果信息$("#result_show").html("");//添加識(shí)別結(jié)果信息$("#result_show").append(inner);});}, false);
</script>

主要python代碼文件如下:

myapp.py:

import json
import os
import timefrom flask import Flask, render_template, request, jsonifyfrom paddleocr import PaddleOCR
from PIL import Image, ImageDraw
import numpy as np# 應(yīng)用名稱,當(dāng)前py名稱,視圖函數(shù)
app = Flask(__name__)# 項(xiàng)目文件夾的絕對路徑
# BASE_DIR = os.path.dirname(os.path.abspath(__name__))
# 相對路徑
BASE_DIR = os.path.dirname(os.path.basename(__name__))# 上傳文件路徑
UPLOAD_DIR = os.path.join(os.path.join(BASE_DIR, 'static'), 'upload')'''
PaddleOCR模型通用識(shí)別方法
'''
def rec_model_ocr(img):# 返回字典結(jié)果對象result_dict = {'result': []}# paddleocr 目前支持的多語言語種可以通過修改lang參數(shù)進(jìn)行切換# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`# 使用CPU預(yù)加載,不用GPU# 模型路徑下必須包含model和params文件,目前開源的v3版本模型 已經(jīng)是識(shí)別率很高的了# 還要更好的就要自己訓(xùn)練模型了。ocr = PaddleOCR(det_model_dir='./inference/ch_PP-OCRv3_det_infer/',rec_model_dir='./inference/ch_PP-OCRv3_rec_infer/',cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer/',use_angle_cls=True, lang="ch", use_gpu=False)# 識(shí)別圖片文件result0 = ocr.ocr(img, cls=True)result = result0[0]for index in range(len(result)):line = result[index]tmp_dict = {}points = line[0]text = line[1][0]score = line[1][1]tmp_dict['points'] = pointstmp_dict['text'] = texttmp_dict['score'] = scoreresult_dict['result'].append(tmp_dict)return result_dict# 轉(zhuǎn)換圖片
def convert_image(image, threshold=None):# 閾值 控制二值化程度,不能超過256,[200, 256]# 適當(dāng)調(diào)大閾值,可以提高文本識(shí)別率,經(jīng)過測試有效。if threshold is None:threshold = 200print('threshold : ', threshold)# 首先進(jìn)行圖片灰度處理image = image.convert("L")pixels = image.load()# 在進(jìn)行二值化for x in range(image.width):for y in range(image.height):if pixels[x, y] > threshold:pixels[x, y] = 255else:pixels[x, y] = 0return image@app.route('/')
def upload_file():return render_template('upload.html')@app.route('/upload/', methods=['GET', 'POST'])
def upload():if request.method == 'POST':# 每個(gè)上傳的文件首先會(huì)保存在服務(wù)器上的臨時(shí)位置,然后將其實(shí)際保存到它的最終位置。filedata = request.files['upload_file']upload_filename = filedata.filenameprint(upload_filename)# 保存文件到指定路徑# 目標(biāo)文件的名稱可以是硬編碼的,也可以從 ?request.files[file] ?對象的? filename ?屬性中獲取。# 但是,建議使用 ?secure_filename()? 函數(shù)獲取它的安全版本if not os.path.exists(UPLOAD_DIR):os.makedirs(UPLOAD_DIR)img_path = os.path.join(UPLOAD_DIR, upload_filename)filedata.save(img_path)print('file uploaded successfully')start = time.time()print('=======開始OCR識(shí)別======')# 打開圖片img1 = Image.open(img_path)# 轉(zhuǎn)換圖片, 識(shí)別圖片文本# print('轉(zhuǎn)換圖片,閾值=220時(shí),再轉(zhuǎn)換為ndarray數(shù)組, 識(shí)別圖片文本')# 轉(zhuǎn)換圖片img2 = convert_image(img1, 220)# Image圖像轉(zhuǎn)換為ndarray數(shù)組img_2 = np.array(img2)# 識(shí)別圖片result_dict = rec_model_ocr(img_2)# 識(shí)別時(shí)間end = time.time()recognize_time = int((end - start) * 1000)result_dict["filename"] = upload_filenameresult_dict["recognize_time"] = str(recognize_time)result_dict["error_code"] = "000000"result_dict["error_msg"] = "識(shí)別成功"# render_template方法:渲染模板# 參數(shù)1: 模板名稱  參數(shù)n: 傳到模板里的數(shù)據(jù)# return render_template('result.html', result_dict=result_dict)# 將數(shù)據(jù)轉(zhuǎn)換成JSON格式,一般用于ajax異步響應(yīng)頁面,不跳轉(zhuǎn)頁面用,等價(jià)下面方法# return json.dumps(result_dict, ensure_ascii=False), {'Content-Type': 'application/json'}# 將數(shù)據(jù)轉(zhuǎn)換成JSON格式,一般用于ajax異步響應(yīng)頁面,不跳轉(zhuǎn)頁面用return jsonify(result_dict)else:return render_template('upload.html')if __name__ == '__main__':# 啟動(dòng)appapp.run(port=8000)

啟動(dòng)flask應(yīng)用,測試結(jié)果如下:

http://www.risenshineclean.com/news/5325.html

相關(guān)文章:

  • 煙臺(tái)優(yōu)化網(wǎng)站建設(shè)長沙seo招聘
  • wordpress關(guān)閉自動(dòng)更新seo診斷分析在線工具
  • 外貿(mào)網(wǎng)站建設(shè)公司流程推廣產(chǎn)品的軟文
  • php動(dòng)態(tài)網(wǎng)站開發(fā)唐四薪課后答案長春網(wǎng)站制作推廣
  • 深圳企業(yè)網(wǎng)站建設(shè)服務(wù)平臺(tái)中國有幾個(gè)搜索引擎
  • 個(gè)人網(wǎng)頁可以做什么內(nèi)容seo網(wǎng)站優(yōu)化方案書
  • 紹興h5建站我想注冊一個(gè)網(wǎng)站怎么注冊
  • 無經(jīng)驗(yàn)做網(wǎng)站今日軍事新聞最新消息新聞
  • java環(huán)境網(wǎng)站整站建設(shè)要怎么網(wǎng)絡(luò)做推廣
  • 專業(yè)的建網(wǎng)站的公司廣州seo網(wǎng)站推廣公司
  • 網(wǎng)站建設(shè)方案選公司十大電商代運(yùn)營公司
  • 網(wǎng)站建設(shè)有免費(fèi)的嗎商丘網(wǎng)站seo
  • 直播網(wǎng)站app開發(fā)開魯網(wǎng)站seo轉(zhuǎn)接
  • 做網(wǎng)絡(luò)課程的網(wǎng)站免費(fèi)站推廣網(wǎng)站不用下載
  • 網(wǎng)站的英文版怎么做的正規(guī)的培訓(xùn)學(xué)校
  • 株洲做網(wǎng)站公司故事式軟文范例500字
  • wordpress熊掌號(hào)專業(yè)版拼多多標(biāo)題關(guān)鍵詞優(yōu)化方法
  • 網(wǎng)站 微信認(rèn)證百度搜索引擎營銷如何實(shí)現(xiàn)
  • 免費(fèi)制作網(wǎng)站和網(wǎng)頁百度上搜索關(guān)鍵詞如何在首頁
  • 哪里可以做網(wǎng)站教程北京seo優(yōu)化廠家
  • 簡單網(wǎng)站建設(shè)公司微信小程序
  • 北京網(wǎng)站建設(shè)制作公司國內(nèi)seo工具
  • 南昌做建網(wǎng)站的整站seo
  • 安徽p2p網(wǎng)站建設(shè)足球進(jìn)球排行榜
  • 電腦網(wǎng)站開發(fā)seo關(guān)鍵詞的優(yōu)化技巧
  • 用服務(wù)器ip怎么做網(wǎng)站電商網(wǎng)絡(luò)營銷
  • 做網(wǎng)站首選九零后網(wǎng)絡(luò)搜索引擎推廣和優(yōu)化方案
  • 做網(wǎng)站有哪些類型的佛山旺道seo
  • 大豐城鄉(xiāng)建設(shè)局網(wǎng)站中文域名注冊管理中心
  • 長春市委網(wǎng)站原畫培訓(xùn)班一般學(xué)費(fèi)多少