Last active
August 2, 2020 14:14
-
-
Save MinaGabriel/a5ae6bf75d7479de864c9dbb32d199c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from absl import app, flags, logging | |
from absl.flags import FLAGS | |
import cv2 | |
import tensorflow as tf | |
from yolov3_tf2.models import ( | |
YoloV3, YoloV3Tiny | |
) | |
from yolov3_tf2.dataset import transform_images | |
from yolov3_tf2.utils import draw_outputs | |
from flask import Flask, render_template, Response | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
# rendering web page | |
# this ill constantly call url_for: | |
return render_template('index.html') | |
def gen(): | |
physical_devices = tf.config.experimental.list_physical_devices('GPU') | |
for physical_device in physical_devices: | |
tf.config.experimental.set_memory_growth(physical_device, True) | |
yolo = YoloV3(classes=80) | |
yolo.load_weights('./checkpoints/yolov3.tf') | |
logging.info('weights loaded') | |
class_names = [c.strip() for c in open('./data/coco.names').readlines()] | |
logging.info('classes loaded') | |
times = [] | |
vid = cv2.VideoCapture('http://192.168.68.122:8080/') | |
while True: | |
_, frame = vid.read() | |
# if frame is None: | |
# logging.warning("Empty Frame") | |
# time.sleep(0.1) | |
# continue | |
img_in = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
img_in = tf.expand_dims(img_in, 0) | |
img_in = transform_images(img_in, 416) | |
t1 = time.time() | |
boxes, scores, classes, nums = yolo.predict(img_in) | |
t2 = time.time() | |
times.append(t2 - t1) | |
times = times[-20:] | |
frame = draw_outputs(frame, (boxes, scores, classes, nums), class_names) | |
frame = cv2.putText(frame, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), | |
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) | |
_, jpeg = cv2.imencode('.jpg', frame) | |
new_frame = jpeg.tobytes() | |
yield (b'--frame\r\n' | |
b'Content-Type: image/jpeg\r\n\r\n' + new_frame + b'\r\n\r\n') | |
@app.route('/video_feed') | |
def video_feed(): | |
return Response(gen(), | |
mimetype='multipart/x-mixed-replace; boundary=frame') | |
if __name__ == "__main__": | |
app.run(host='192.168.68.126', port='5000', debug=True) | |
###############HTML PAGE######################## | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" | |
content="width=device-width, initial-scale=1, user-scalable=yes"> | |
<link href="/static/css/style.css" rel="stylesheet"> | |
<title></title> | |
</head> | |
<body> | |
<h1>Detection</h1> | |
<img id="bg" src="{{ url_for('video_feed') }}"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment