File size: 5,725 Bytes
4f0b54e
 
 
 
 
 
 
 
 
 
 
 
 
074135b
4f0b54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd272d
4f0b54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
from flask import Flask, render_template, request, redirect, url_for,send_from_directory
import cv2
import numpy as np
from transformers import DetrImageProcessor, DetrForObjectDetection
from torchvision.transforms import functional as F
from ultralytics import YOLO
import torch



app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS



@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


@app.route('/', methods=['GET', 'POST'])
def index():
    annotated_image_url = None

    if request.method == 'POST':

        # Load the YOLOv8 model
        yolo_model = YOLO('yolo/best.pt')

        # Load the DETR model
        processor = DetrImageProcessor.from_pretrained("detr")
        model = DetrForObjectDetection.from_pretrained("detr")

        # Check if a file is selected
        if 'image' not in request.files:
            return redirect(request.url)
        
        image = request.files['image']
        
        # Check if the file has a valid extension
        if image and allowed_file(image.filename):
            constant_filename = 'my_uploaded_image.jpg'  # Specify the constant name
            filename = os.path.join(app.config['UPLOAD_FOLDER'], constant_filename)
            image.save(filename)

            # Load the image for processing
            image = cv2.imread(filename)

            # Perform YOLO object detection and annotation
            yolo_results = yolo_model(image, save=False)
            yolo_image = image.copy()
            yolo_names=yolo_results[0].names
            for row in yolo_results[0].boxes.data:
                x1, y1, x2, y2, score, class_id = row.tolist()
                x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])

                class_name = yolo_names.get(int(class_id), 'Unknown')
                label_text = f"Class: {class_name}, Score: {score:.2f}"
                box_color = (0, 0, 255)
                label_color = (255, 255, 255)

                cv2.rectangle(yolo_image, (x1, y1), (x2, y2), box_color, thickness=2)
                label_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)[0]
                label_bottom_left = (x1, y1 - 5)
                label_top_right = (label_bottom_left[0] + label_size[0], label_bottom_left[1] - label_size[1])
                cv2.rectangle(yolo_image, label_bottom_left, label_top_right, box_color, cv2.FILLED)
                cv2.putText(yolo_image, label_text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, label_color, 1, cv2.LINE_AA)




            annotated_filename = 'annotated_my_uploaded_image.jpg'
            annotated_filepath = os.path.join(app.config['UPLOAD_FOLDER'], annotated_filename)
            cv2.imwrite(annotated_filepath, yolo_image)
            annotated_image_url = url_for('uploaded_file', filename=annotated_filename)




            
# Process the image using the processor
            inputs = processor(images=image, return_tensors="pt")
            outputs = model(**inputs)

            # Convert outputs (bounding boxes and class logits) to COCO API format
            # Let's only keep detections with score > 0.9
            target_sizes = torch.tensor([image.shape[:2:]])
            results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.3)[0]

            # Convert PIL image to NumPy array for OpenCV
            #image_np = np.array(image)
            #image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
            image_cv2 = image.copy()

            # Define the font for labels
            font = cv2.FONT_HERSHEY_SIMPLEX
            font_scale = 0.5
            font_thickness = 1
            font_color = (255, 255, 255)  # White color

            # Iterate over the results and draw bounding boxes and labels using OpenCV
            for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
                box = [round(i, 2) for i in box.tolist()]

                # Draw the bounding box
                box = [int(b) for b in box]  # Convert to integers for drawing
                cv2.rectangle(image_cv2, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2)  # Red rectangle

                # Draw the label
                label_text = f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}"
                label_size = cv2.getTextSize(label_text, font, font_scale, font_thickness)[0]
                label_bottom_left = (box[0], box[1] - 5)  # Adjust label position
                label_top_right = (label_bottom_left[0] + label_size[0], label_bottom_left[1] - label_size[1])
                cv2.rectangle(image_cv2, label_bottom_left, label_top_right, (0, 0, 255), cv2.FILLED)  # Red filled rectangle
                cv2.putText(image_cv2, label_text, (box[0], box[1] - 5), font, font_scale, font_color, font_thickness, cv2.LINE_AA)


            annotated_filename = 'dert_annotated_my_uploaded_image.jpg'
            annotated_filepath = os.path.join(app.config['UPLOAD_FOLDER'], annotated_filename)
            cv2.imwrite(annotated_filepath, image_cv2)
            dertannotated_image_url = url_for('uploaded_file', filename=annotated_filename)





            return render_template('index.html', image1=annotated_image_url ,image2= dertannotated_image_url)









        
    return render_template('index.html', image1=annotated_image_url,image2=annotated_image_url)



if __name__ == '__main__':
    app.run(debug=True,port=7860)