Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| from detectron2.config import get_cfg | |
| from detectron2 import model_zoo | |
| from detectron2.engine import DefaultPredictor | |
| from detectron2.utils.visualizer import Visualizer | |
| from detectron2.data import MetadataCatalog | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| import os | |
| # Dropbox model link | |
| MODEL_URL = "https://www.dropbox.com/scl/fi/m8e7tr4vy887rrmedvpok/model_final-1.pth?rlkey=bf5ov8r1m89u9qp88alpuvmse&st=htkj8ux1&dl=1" | |
| MODEL_PATH = "model_final.pth" | |
| # Download model if not exists | |
| if not os.path.exists(MODEL_PATH): | |
| print("Downloading model...") | |
| response = requests.get(MODEL_URL, stream=True) | |
| with open(MODEL_PATH, 'wb') as f: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| f.write(chunk) | |
| print("Model downloaded.") | |
| # Configure Detectron2 | |
| cfg = get_cfg() | |
| cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) | |
| cfg.MODEL.ROI_HEADS.NUM_CLASSES = 8 | |
| cfg.MODEL.WEIGHTS = MODEL_PATH | |
| cfg.MODEL.DEVICE = "cpu" # Set to CPU for Hugging Face Spaces | |
| predictor = DefaultPredictor(cfg) | |
| # Metadata | |
| MetadataCatalog.get("car_parts").set(thing_classes=[ | |
| "Dent", "Scratch", "Broken part", "Paint chip", | |
| "Missing part", "Flaking", "Corrosion", "Cracked" | |
| ]) | |
| metadata = MetadataCatalog.get("car_parts") | |
| def detect_damage(input_image): | |
| # Convert PIL image to OpenCV format | |
| image_cv2 = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR) | |
| # Run predictions | |
| outputs = predictor(image_cv2) | |
| # Visualize predictions | |
| v = Visualizer(image_cv2[:, :, ::-1], metadata=metadata, scale=1.0) | |
| out = v.draw_instance_predictions(outputs["instances"].to("cpu")) | |
| visualized_image = out.get_image()[:, :, ::-1] | |
| # Convert back to PIL for display in Gradio | |
| return Image.fromarray(visualized_image) | |
| # Gradio Interface | |
| demo = gr.Interface( | |
| fn=detect_damage, | |
| inputs=gr.Image(type="pil"), | |
| outputs="image", | |
| title="Car Parts Damage Detection", | |
| description="Upload an image of a car to detect damage such as dents, scratches, and broken parts." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |