HassanDataSci commited on
Commit
104786a
·
verified ·
1 Parent(s): 7894c61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -68
app.py CHANGED
@@ -1,69 +1,26 @@
1
- import gradio as gr
2
- import cv2
3
- import torch
4
- from transformers import DetrImageProcessor, DetrForObjectDetection
5
  from PIL import Image
6
- import tempfile
7
- import os
8
-
9
- # Load the DETR processor and model
10
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
11
- model = DetrForObjectDetection.from_pretrained("ArrayDice/Vehicle_Detection_Model_Zoom")
12
-
13
- # Function to process video and detect vehicles
14
- def detect_vehicles_in_video(video_path):
15
- # Load video
16
- cap = cv2.VideoCapture(video_path)
17
- fps = cap.get(cv2.CAP_PROP_FPS)
18
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
19
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
20
-
21
- # Temporary output video file
22
- temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
23
- out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
24
-
25
- # Process each frame
26
- while cap.isOpened():
27
- ret, frame = cap.read()
28
- if not ret:
29
- break
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)