Spaces:
Runtime error
Runtime error
main file
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import imutils
|
| 6 |
+
import easyocr
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 9 |
+
|
| 10 |
+
# Load the model and image processor
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained("beingamit99/car_damage_detection")
|
| 12 |
+
model = AutoModelForImageClassification.from_pretrained("beingamit99/car_damage_detection")
|
| 13 |
+
|
| 14 |
+
# Define the function that takes an image as input and returns a text output
|
| 15 |
+
def classify_image(input_image):
|
| 16 |
+
# Load and process the image
|
| 17 |
+
image = np.array(input_image)
|
| 18 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# Make predictions
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
logits = outputs.logits.detach().cpu().numpy()
|
| 23 |
+
predicted_class_id = np.argmax(logits)
|
| 24 |
+
predicted_proba = np.max(logits)
|
| 25 |
+
label_map = model.config.id2label
|
| 26 |
+
predicted_class_name = label_map[predicted_class_id]
|
| 27 |
+
|
| 28 |
+
# OCR
|
| 29 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
bfilter = cv2.bilateralFilter(gray, 11, 17, 17)
|
| 31 |
+
edged = cv2.Canny(bfilter, 30, 200)
|
| 32 |
+
keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 33 |
+
contours = imutils.grab_contours(keypoints)
|
| 34 |
+
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
|
| 35 |
+
location = None
|
| 36 |
+
for contour in contours:
|
| 37 |
+
approx = cv2.approxPolyDP(contour, 10, True)
|
| 38 |
+
if len(approx) == 4:
|
| 39 |
+
location = approx
|
| 40 |
+
break
|
| 41 |
+
mask = np.zeros(gray.shape, np.uint8)
|
| 42 |
+
new_image = cv2.drawContours(mask, [location], 0, 255, -1)
|
| 43 |
+
new_image = cv2.bitwise_and(image, image, mask=mask)
|
| 44 |
+
(x, y) = np.where(mask == 255)
|
| 45 |
+
(x1, y1) = (np.min(x), np.min(y))
|
| 46 |
+
(x2, y2) = (np.max(x), np.max(y))
|
| 47 |
+
cropped_image = gray[x1:x2+3, y1:y2+3]
|
| 48 |
+
reader = easyocr.Reader(['en'])
|
| 49 |
+
result = reader.readtext(cropped_image)
|
| 50 |
+
text = result[0][1]
|
| 51 |
+
|
| 52 |
+
# Return the results
|
| 53 |
+
return f"Predicted class: {predicted_class_name} (probability: {predicted_proba:.4f}", text
|
| 54 |
+
|
| 55 |
+
# Create Gradio interface
|
| 56 |
+
input_image = gr.components.Image()
|
| 57 |
+
output_text = gr.components.Text()
|
| 58 |
+
output_text2 = gr.components.Text()
|
| 59 |
+
|
| 60 |
+
gr.Interface(fn=classify_image, inputs=input_image, outputs=[output_text, output_text2], title="AutoVision").launch(debug = 1)
|