Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,26 @@
|
|
1 |
-
import
|
2 |
-
import cv2
|
3 |
-
import torch
|
4 |
-
from transformers import DetrImageProcessor, DetrForObjectDetection
|
5 |
from PIL import Image
|
6 |
-
import
|
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 |
-
# Convert frame to PIL for model processing
|
32 |
-
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
33 |
-
|
34 |
-
# Run object detection
|
35 |
-
inputs = processor(images=pil_image, return_tensors="pt")
|
36 |
-
outputs = model(**inputs)
|
37 |
-
|
38 |
-
# Set confidence threshold and draw boxes
|
39 |
-
for score, label, box in zip(outputs.logits.softmax(-1)[0], outputs.labels[0], outputs.boxes[0]):
|
40 |
-
if score.max() >= 0.9: # Adjust confidence threshold as needed
|
41 |
-
x_min, y_min, x_max, y_max = map(int, box)
|
42 |
-
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
43 |
-
label_text = f"Vehicle ({score.max():.2f})"
|
44 |
-
cv2.putText(frame, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
45 |
-
|
46 |
-
# Write annotated frame to output video
|
47 |
-
out.write(frame)
|
48 |
-
|
49 |
-
# Release resources
|
50 |
-
cap.release()
|
51 |
-
out.release()
|
52 |
-
|
53 |
-
return temp_video.name # Return path to the annotated video
|
54 |
-
|
55 |
-
# Gradio interface
|
56 |
-
def vehicle_detection_gradio(video):
|
57 |
-
annotated_video_path = detect_vehicles_in_video(video.name)
|
58 |
-
return annotated_video_path
|
59 |
-
|
60 |
-
# Set up Gradio interface
|
61 |
-
iface = gr.Interface(
|
62 |
-
fn=vehicle_detection_gradio,
|
63 |
-
inputs=gr.Video(type="file", label="Upload a video for vehicle detection"),
|
64 |
-
outputs=gr.Video(label="Annotated Video with Detected Vehicles"),
|
65 |
-
title="Vehicle Detection Model",
|
66 |
-
description="Upload a video, and the model will detect and annotate vehicles in each frame."
|
67 |
-
)
|
68 |
-
|
69 |
-
iface.launch()
|
|
|
1 |
+
import numpy as np
|
|
|
|
|
|
|
2 |
from PIL import Image
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
+
|
5 |
+
# Load the model and image processor
|
6 |
+
processor = AutoImageProcessor.from_pretrained("beingamit99/car_damage_detection")
|
7 |
+
model = AutoModelForImageClassification.from_pretrained("beingamit99/car_damage_detection")
|
8 |
+
|
9 |
+
# Load and process the image
|
10 |
+
image = Image.open(IMAGE)
|
11 |
+
inputs = processor(images=image, return_tensors="pt")
|
12 |
+
|
13 |
+
# Make predictions
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits.detach().cpu().numpy()
|
16 |
+
predicted_class_id = np.argmax(logits)
|
17 |
+
predicted_proba = np.max(logits)
|
18 |
+
label_map = model.config.id2label
|
19 |
+
predicted_class_name = label_map[predicted_class_id]
|
20 |
+
|
21 |
+
# Print the results
|
22 |
+
print(f"Predicted class: {predicted_class_name} (probability: {predicted_proba:.4f}")
|
23 |
+
from transformers import pipeline
|
24 |
+
#Create a classification pipeline
|
25 |
+
pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
|
26 |
+
pipe(IMAGE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|