1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| import sys import subprocess import os from PIL import Image from datetime import datetime from ASR_metrics import utils as metrics
from werkzeug.wrappers import Request, Response from flask import Flask, render_template, request, jsonify
sys.path.append('/home/nvidia/7th_CV') sys.path.append('/home/nvidia/7th_ASR')
pathASR = "/home/nvidia/7th_ASR"
pathSky = '/home/nvidia'
app = Flask(__name__, static_folder='')
uploadPath = 'uploads/' try_model_1 = None
@app.route('/') def index(): return render_template('sky7.html', template_folder='templates')
@app.route('/asr/load') def asrLoad(): global try_model_1 if try_model_1 == None: import nemo.collections.asr as nemo_asr print('Loading Nemo') try_model_1 = nemo_asr.models.EncDecCTCModel.restore_from("/home/nvidia/7th_ASR/7th_asr_model.nemo") print('Done loading Nemo') return 'ok'
@app.route('/asr/upload', methods=['POST']) def asrUpload(): if request.method == 'POST': f = request.files['file'] if(f.headers.get('Content-Type') != 'audio/wav'): return '音频格式有错误', 400 else: fileName = f'{uploadPath}audio.wav' f.save(fileName) dt = datetime.now() ts = str(int(datetime.timestamp(dt))) return jsonify(f'/{uploadPath}audio.wav?t={ts}')
@app.route('/asr/identify', methods=['GET', 'POST']) def asrIdentify(): global try_model_1 if try_model_1 == None: return '模型无效,请重新加载', 500 try: asr_result = try_model_1.transcribe(paths2audio_files=["uploads/audio.wav"]) s1 = request.form.get('defaultText') s2 = " ".join(asr_result) result = { "asr_result": asr_result, "word_error_rate": metrics.calculate_cer(s1,s2), "word_accuracy_rate":1-metrics.calculate_cer(s1,s2) } return jsonify(result) except Exception as e: return '无法识别', 400
@app.route("/cv/upload", methods=['POST']) def cvUpload(): if request.method == 'POST': f = request.files['file'] print('image', f, f.filename)
if not 'image' in f.headers.get('Content-Type'): return '图片有错误', 400
original = f'{uploadPath}original.jpg'
try: im = Image.open(f) rgb_im = im.convert('RGB') rgb_im.save(original)
dt = datetime.now() ts = str(int(datetime.timestamp(dt))) return jsonify(original+'?t='+ts)
except Exception as e: return '有错误', 400
@app.route("/api/detect/image") def detectImage(): cv_results = subprocess.Popen('python3 /home/nvidia/7th_CV/detection_image.py /home/nvidia/uploads/original.jpg', shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) print('code', cv_results.returncode) cv_results = str(cv_results.stdout.read()).split('\\n')[-2] dt = datetime.now() ts = str(int(datetime.timestamp(dt))) result = { "detection_result_image_path": f'/uploads/result.jpg?t={ts}' } return jsonify(result)
@app.route("/api/detect/fps") def detectFPS(): fps_results = subprocess.Popen('python3 /home/nvidia/7th_CV/cv_fps.py', shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) fps_results = str(fps_results.stdout.read()).split('\\n')[-2] fps_results = fps_results.split(" ")[-1] result = { "detection_FPS": fps_results, } return jsonify(result)
@app.route("/api/detect/map") def detectMAP(): map_results = subprocess.Popen('python3 /home/nvidia/7th_CV/cv_map.py', shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) map_results = str(map_results.stdout.read()) bytes(map_results, encoding="utf-8").decode() map_results = map_results[-9:-3] result = { "detection_mAP": map_results, } return jsonify(result)
if __name__ == "__main__": app.run(debug=True)
|