Skip to content

Commit

Permalink
css + backend spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasham268 committed May 31, 2024
1 parent 5a150f2 commit 7a96a61
Show file tree
Hide file tree
Showing 13 changed files with 10,500 additions and 9,316 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ npm-debug.log.*
# eslint ignores hidden directories by default:
# https://github.com/eslint/eslint/issues/8429
!.erb


venv
58 changes: 58 additions & 0 deletions flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import base64
import cv2
import numpy as np
from flask import Flask, request, jsonify
from flask_cors import CORS
from PIL import Image
import io
from io import BytesIO
from ultralytics import YOLO

app = Flask(__name__)
CORS(app, origins=['*'])

model = YOLO('./model.pt')
model2 = YOLO('./yolov8n.pt')

@app.route('/detect', methods=['POST'])
def detect():
if 'image' not in request.files:
return jsonify({"error": "No image provided"}), 400
try:
image_file = request.files['image']
image = Image.open(image_file)
image_array = np.array(image)

results = model(image_array, conf=0.9)
output = results[0].plot()

_, buffer = cv2.imencode('.jpg', output)
detected_image_base64 = base64.b64encode(buffer).decode('utf-8')

return jsonify({"detectedImage": detected_image_base64})
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route('/detectVideo', methods=['POST'])
def detect_objects():
if 'frame' not in request.files:
return jsonify({"error": "No frame part in the request"}), 400

try:
frame_file = request.files['frame']
frame_image = Image.open(frame_file.stream)
frame_array = np.array(frame_image)

results = model2(frame_array)
output = results[0].plot()

_, buffer = cv2.imencode('.jpg', output)
processed_frame_base64 = base64.b64encode(buffer).decode('utf-8')

return jsonify({"processed_frame": processed_frame_base64})
except Exception as e:
return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
app.run(debug=True)
Binary file added flask/model.pt
Binary file not shown.
Binary file added flask/yolov8n.pt
Binary file not shown.
Loading

0 comments on commit 7a96a61

Please sign in to comment.