【发布时间】:2021-08-21 13:19:06
【问题描述】:
我的应用程序所做的是捕获实时图像并使用 google vision ocr 提取一些值。 我从外部 python 脚本获取数据,我想在 html 文本字段中显示输出。
这是我的 views.py 模板
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.http import JsonResponse
from .models import TemperatureRecordsF, TemperatureRecordC , BloodPressureRecord , SPO2Levels
import sys
import base64
from PIL import Image
from subprocess import run, PIPE
def button(request, *args, **kwargs):
return render(request,'capturr1.html',)
def external(request, *args, **kwargs):
request_getdata = request.POST.get('img64',None)
selected_radio = request.POST.get('radio_seleect',None)
print(selected_radio)
# print(request_getdata)
headed, encoded = request_getdata.split(",",1)
data = base64.b64decode(encoded)
with open("D:/dev/django/src/OCR/media/image.png", "wb") as f:
f.write(data)
path = 'D:/dev/django/src/OCR/media/image.png'
out = run([sys.executable,'D://dev//django//src//OCR//detect_text.py',path],shell=False,stdout=PIPE)
print(out.stdout)
context = {
'output' : out.stdout
}
return render(request,'capturr1.html',context)
这是我的模板 capturr1.html
<form class="contentarea" action="/external" method="POST" id="capture" name="capture">
{% csrf_token %}
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
<label class="form-check-label" for="inlineRadio1">Temperature °F</label>
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
<label class="form-check-label" for="inlineRadio2">Temperature °C</label>
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3">
<label class="form-check-label" for="inlineRadio3">Blood Pressure</label>
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio4" value="option4">
<label class="form-check-label" for="inlineRadio4">Oxygen Saturation Levels</label><br>
<video id="video">Video stream not available.</video><br>
<button id="startbutton">Take photo</button>
<input type="submit" value="Save" class="save" name="savebutton"/>
<input type="text" />{{ output }}
<canvas id="canvas" style="display: none;"><br>
</canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box."><br/>
</div>
</form>
这是我的 ajax 请求。我正在将我的画布图像数据和选定的单选按钮值发送到 django。
$(document).ready(function() {
$("#capture").submit(function(event) {
var radio_select = document.capture.inlineRadioOptions.value;
console.log(radio_select)
event.preventDefault();
$.ajax({
type: "POST",
url: 'external',
data: {
'img64': photo.src,
'radio_seleect': radio_select
}
}).done(function() {
console.log('sent');
});
});
});
【问题讨论】:
-
错误是什么?
-
没有得到任何错误,上下文没有显示在 html 模板中
-
print(out.stdout)这是打印一些值吗? -
是的。它将打印在控制台上
-
你有没有尝试在浏览器和python中都使用调试模式来看看各自做了什么?
标签: javascript html django django-views render