TIF_E41201693/app.py

96 lines
2.9 KiB
Python

from flask import Flask, render_template, Response, jsonify
import numpy as np
import os
import cv2
import tensorflow as tf
from keras import models, utils, backend as K
from custom_object import YOLOLoss, YOLOActivation
from function import predictImage
import time
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
app = Flask(__name__)
camera = cv2.VideoCapture(0)
bio = [
{
'penulis': 'Adilah Qurrotu\'aini',
'angkatan': '2020',
'prodi': 'Teknik Informatika',
'jurusan': 'Teknologi Informasi',
'ptn': 'Politeknik Negeri Jember',
'email': 'adilah.qurrotu.aini@gmail.com'
}
]
# Load YOLOv2 model
model = tf.keras.models.load_model('model/SIBIDetection_YOLOv2_780_2.h5', custom_objects={'YOLOLoss': YOLOLoss, 'YOLOActivation': YOLOActivation})
# Load hand gesture recognition model
# gesture_model = tf.keras.models.load_model('model/SIBIDetection_YOLOv2_780_2.h5')
# Load anchor box dimensions
anchor_box_dimensions = np.array([[7.34375, 7.96875],
[6.65625, 10.46875],
[9.28125, 10.40625],
[5.34375, 9.21875],
[10.28125, 6.65625]])
def generate_frames():
global prediction_text
while True:
success, frame = camera.read()
if not success:
break
else:
grayscale_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
grayscale_frame = cv2.cvtColor(grayscale_frame, cv2.COLOR_GRAY2BGR)
predicted_img, class_pred = predictImage(model, grayscale_frame, threshold = 0.1)
ret, buffer = cv2.imencode('.jpg', predicted_img)
frame = buffer.tobytes()
prediction_text = chr(65 + class_pred)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
current_index = 0
# Fungsi untuk melakukan streaming teks
def generate_text():
global prediction_text
yield prediction_text
@app.route('/stream')
def stream():
return Response(generate_text(), mimetype='text/html')
@app.route('/video')
def video():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/fetchtext')
def fetchtext():
#return jsonify({'prediction': prediction_text})
return Response(generate_text(), content_type='text/plain')
@app.route('/')
def home():
return render_template('home.html', title='Beranda')
@app.route('/dictionary')
def dictionary():
return render_template('dictionary.html', title='Kamus Isyarat')
@app.route('/detection')
def detection():
return render_template('detection.html', title='Deteksi Isyarat')
@app.route('/about')
def about():
return render_template('about.html', title='Tentang Penulis', bio=bio)
if __name__ == "__main__":
app.run(debug=True)