CoolFace
Apppublic

GURUAKASH/ROADMONITORING

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
app.py43 linesDownload Raw Back to root
1from flask import Flask, render_template, request, send_file2import cv23import os4 5app = Flask(__name__)6 7# Function to perform object detection8def detect_objects(image_path):9    img = cv2.imread(image_path)10 11    with open(os.path.join("project_files", 'obj.names'), 'r') as f:12        classes = f.read().splitlines()13 14    net = cv2.dnn.readNet('project_files/yolov4_tiny.weights', 'project_files/yolov4_tiny.cfg')15    model = cv2.dnn_DetectionModel(net)16    model.setInputParams(scale=1 / 255, size=(416, 416), swapRB=True)17    classIds, scores, boxes = model.detect(img, confThreshold=0.6, nmsThreshold=0.4)18 19    for (classId, score, box) in zip(classIds, scores, boxes):20        cv2.rectangle(img, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]),21                      color=(0, 255, 0), thickness=2)22 23    result_path = "static/result.jpg"24    cv2.imwrite(result_path, img)25    return result_path26 27@app.route("/", methods=["GET", "POST"])28def index():29    if request.method == "POST":30        # Save the uploaded image31        f = request.files['file']32        image_path = "static/p1.jpg"33        f.save(image_path)34 35        # Perform object detection36        result_path = detect_objects(image_path)37        return render_template("result.html", result_path=result_path)38 39    return render_template("index.html")40 41if __name__ == "__main__":42    app.run(debug=True)43