67 lines
1.5 KiB
Python
67 lines
1.5 KiB
Python
|
import cv2
|
||
|
import numpy as np
|
||
|
import json
|
||
|
|
||
|
from flask import Flask, url_for
|
||
|
app = Flask("Kotely")
|
||
|
|
||
|
@app.route('/')
|
||
|
def api_root():
|
||
|
return 'Welcome'
|
||
|
|
||
|
@app.route('/image/<path:imagepath>')
|
||
|
def info(imagepath):
|
||
|
return recognition("/"+imagepath)
|
||
|
|
||
|
def get_output_layers(net):
|
||
|
layer_names = net.getLayerNames()
|
||
|
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
|
||
|
return output_layers
|
||
|
|
||
|
def recognition(photo):
|
||
|
image = cv2.imread(photo)
|
||
|
|
||
|
Width = image.shape[1]
|
||
|
Height = image.shape[0]
|
||
|
scale = 0.00392
|
||
|
|
||
|
with open("yolov3.txt", 'r') as f:
|
||
|
classes = [line.strip() for line in f.readlines()]
|
||
|
|
||
|
COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
|
||
|
|
||
|
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
|
||
|
|
||
|
blob = cv2.dnn.blobFromImage(image, scale, (416, 416), (0, 0, 0), True, crop=False)
|
||
|
|
||
|
net.setInput(blob)
|
||
|
|
||
|
outs = net.forward(get_output_layers(net))
|
||
|
|
||
|
class_ids = []
|
||
|
confidences = []
|
||
|
boxes = []
|
||
|
conf_threshold = 0.5
|
||
|
nms_threshold = 0.4
|
||
|
|
||
|
for out in outs:
|
||
|
for detection in out:
|
||
|
scores = detection[5:]
|
||
|
class_id = np.argmax(scores)
|
||
|
confidence = scores[class_id]
|
||
|
if confidence > 0.5:
|
||
|
class_ids.append(class_id)
|
||
|
data = '{"path":"'+photo+'"}'
|
||
|
js = json.loads(data)
|
||
|
|
||
|
if 15 in class_ids:
|
||
|
js.update({"cat":1})
|
||
|
return js
|
||
|
|
||
|
else:
|
||
|
js.update({"cat": 0})
|
||
|
return js
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(port="8809")
|
||
|
|