python计算机视觉项目供Java后端调用

项目背景:目前开发一个媒体APP,然后需要我的视频检测功能。目前功能代码已经完成,需要我写API供Java后端调用。这是我第一次进入项目开发组,不明白后端到底要的是什么。Java后端的口述:“我需要你提供一个调用链接,json格式的啥的,你的代码可以本地让我调用,也可以部署到服务器,具体的怎么操作你就自己多在网上看看”。

python功能代码块:


import cv2
import numpy as np

path = 'D:/Python/Administrator/PycharmProjects/pythonProject/视频识别/yoloProject/imgs/huaxue.mp4'
cap = cv2.VideoCapture(path)

def findObjects(outputs, img):
    classesFile = 'coco.names'
    classNames = []
    with open(classesFile, 'rt') as f:
        classNames = f.read().rstrip('\n').split('\n')
    # print(classNames)
    # print("Class number: ", len(classNames))

    # 非最大值抑制阈值
    threshold = 0.1

    height, width, channel = img.shape
    boundingBox = []
    classIDs = []
    confidences = []
    for outputs in outputs:
        for det in outputs:
            scores = det[5:]
            classID = np.argmax(scores)
            confidence = scores[classID]

            if confidence > threshold:
                w, h = int(det[2]*width), int(det[3]*height)  #宽和高比例
                x, y = int((det[0]*width) - w/2), int(det[1]*height - h/2)  # 中心点
                boundingBox.append([x, y, w, h])
                classIDs.append(classID)
                confidences.append(float(confidence))
    # print(len(boundingBox))
    indices = cv2.dnn.NMSBoxes(boundingBox, confidences, threshold, nms_threshold=0.2)
    for i in indices:
        i = i[0]
        box = boundingBox[i]
        x,y,w,h = box[0],box[1],box[2],box[3]
        # 显示画框字体大小颜色
        cv2.rectangle(img, (x,y),(x+w, y+h),(255,0,255),2)
        cv2.putText(img,f'{classNames[classIDs[i]].upper()} {int(confidences[i]*100)}%',
                    (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,255),2) # 显示标签字体大小颜色
        # 返回识别框的文本标签
        return print(classNames[classIDs[i]].upper())

# 配置文件
modelConfiguration = 'yolov3-tiny.cfg'
# 权重文件
modelWeights = 'yolov3-tiny.weights'

# 加载网络,配置权重
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
# 设定前向计算的硬件平台为CPU,也可采用GUP加速计算
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

while True:
    ret, frame = cap.read()  ##ret返回布尔量
    # 窗口大小
    frame = cv2.resize(frame, (900, 700))

    blob = cv2.dnn.blobFromImage(frame, 1 / 255, (320, 320), [0, 0, 0], 1, crop=False)
    net.setInput(blob)

    layerNames = net.getLayerNames()
    # print(layerNames)
    outputNames = [layerNames[i[0]-1] for i in net.getUnconnectedOutLayers()]
    #print(outputNames)  # 查找输出层 [3 个输出层]
    # 设置输出
    outputs = net.forward(outputNames)

    findObjects(outputs, frame)
    cv2.imshow('image', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

flask代码块:

from flask import Flask,jsonify,request

app = Flask(__name__)

# 设置API
@app.route('/')

def index():
    return {
        "msg": "success",
        "data": "welcom."
    }


data = {'huhy':{'age':24,'sex':'女'},
        'liuer':{'age':12,'sex':'男'}
        }
@app.route("/login", methods = ["POST"])

def login():
    # 功能代码

    return jsonify(data)

if __name__ == "__main__":
    app.run(host='192.168.0.38', port='1083')


问题一: 第一次接触项目开发只知道他要的是一个链接,我可以返回的也只是一个本地的链接,不明白后端同事要的是什么样的,求指点。

问题二:我尝试过把代码块一放到代码块二中的功能代码处,打开网页后显示无法打开问题,想知道啥原因.

问题三:有在网上找相关案例,但是都是和代码块中的index函数一样简单的。想知道怎么样把我的代码块一放入代码块二中,并可在网址中返回输出.

里面的yolov3相关文件也是齐全的,如有需要测试,到时候我发出来. 谢谢大家了

麻烦指点啊