Prathamesh1420's picture
Create app.py
b3e6c70 verified
'''from flask import Flask, render_template, request
import cv2
import numpy as np
import base64
import re
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
if 'image' not in data:
return 'No image', 400
image_data = re.sub('^data:image/.+;base64,', '', data['image'])
img_bytes = base64.b64decode(image_data)
np_img = np.frombuffer(img_bytes, dtype=np.uint8)
frame = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
# Process frame here (e.g., face detection)
print("Received a frame of shape:", frame.shape)
return 'OK', 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)'''
'''
import cv2
import numpy as np
import base64
from flask import Flask, request, jsonify, render_template
from ultralytics import YOLO
app = Flask(__name__)
model = YOLO('yolov8n.pt') # Load the YOLO model
@app.route('/')
def index():
return render_template('index.html') # Your HTML file
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
image_data = data['image'].split(',')[1]
img_bytes = base64.b64decode(image_data)
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# Run YOLO detection
results = model(frame)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# (Optional) Save or return detections if needed
return jsonify({"status": "success"})
if __name__ == '__main__':
app.run(debug=True)
'''
'''import cv2
import numpy as np
import base64
from flask import Flask, request, jsonify, render_template
from ultralytics import YOLO
app = Flask(__name__)
model = YOLO('yolov8n.pt')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
image_data = data['image'].split(',')[1]
img_bytes = base64.b64decode(image_data)
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# Run YOLO detection
results = model(frame)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Encode the frame back to JPEG
_, buffer = cv2.imencode('.jpg', frame)
annotated_base64 = base64.b64encode(buffer).decode('utf-8')
return jsonify({"status": "success", "image": f"data:image/jpeg;base64,{annotated_base64}"})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5001, debug=True)'''
'''import cv2
import numpy as np
import base64
from flask import Flask, request, jsonify, render_template
from ultralytics import YOLO
app = Flask(__name__)
# Load YOLOv8 model (e.g., yolov8n.pt, yolov8s.pt, etc.)
model = YOLO('yolov8n.pt')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
if 'image' not in data:
return jsonify({'error': 'No image provided'}), 400
# Decode base64 image
image_data = data['image'].split(',')[1]
img_bytes = base64.b64decode(image_data)
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# Run YOLO detection
results = model(frame)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Coordinates
class_id = int(box.cls[0]) # Class ID
class_name = model.names[class_id] # Class name
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Put class name text
cv2.putText(
frame,
class_name,
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0, 255, 0),
2
)
# Encode annotated image back to base64
_, buffer = cv2.imencode('.jpg', frame)
annotated_base64 = base64.b64encode(buffer).decode('utf-8')
return jsonify({
"status": "success",
"image": f"data:image/jpeg;base64,{annotated_base64}"
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)'''
'''import cv2
import numpy as np
import base64
from flask import Flask, request, jsonify, render_template
from ultralytics import YOLO
app = Flask(__name__)
model = YOLO('yolov8n.pt') # Load YOLOv8 model
CONFIDENCE_THRESHOLD = 0.8 # Set confidence threshold (0.0 to 1.0)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
if 'image' not in data:
return jsonify({'error': 'No image provided'}), 400
# Decode base64 image
image_data = data['image'].split(',')[1]
img_bytes = base64.b64decode(image_data)
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# Run YOLO detection
results = model(frame)
for result in results:
for box in result.boxes:
if box.conf[0] < CONFIDENCE_THRESHOLD:
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
class_id = int(box.cls[0])
class_name = model.names[class_id]
confidence = float(box.conf[0])
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Draw class name + confidence
label = f"{class_name} {confidence:.2f}"
cv2.putText(frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Encode the frame back to base64
_, buffer = cv2.imencode('.jpg', frame)
annotated_base64 = base64.b64encode(buffer).decode('utf-8')
return jsonify({
"status": "success",
"image": f"data:image/jpeg;base64,{annotated_base64}"
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)
'''
import cv2
import numpy as np
import base64
from flask import Flask, request, jsonify, render_template
from ultralytics import YOLO
app = Flask(__name__)
model = YOLO('yolov8n.pt') # Replace with your desired YOLO model
CONFIDENCE_THRESHOLD = 0.5
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_frame', methods=['POST'])
def upload_frame():
data = request.get_json()
if 'image' not in data:
return jsonify({'error': 'No image provided'}), 400
# Decode base64 image
image_data = data['image'].split(',')[1]
img_bytes = base64.b64decode(image_data)
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# Run YOLO detection
results = model(frame)
for result in results:
for box in result.boxes:
if box.conf[0] < CONFIDENCE_THRESHOLD:
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
class_id = int(box.cls[0])
class_name = model.names[class_id]
confidence = float(box.conf[0])
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Draw class name + confidence
label = f"{class_name} {confidence:.2f}"
cv2.putText(frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Encode the frame back to base64
_, buffer = cv2.imencode('.jpg', frame)
annotated_base64 = base64.b64encode(buffer).decode('utf-8')
return jsonify({
"status": "success",
"image": f"data:image/jpeg;base64,{annotated_base64}"
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)