Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233import osimport gradio as grfrom transformers import pipelinefrom transformers import DetrForSegmentation, DetrConfig# Initialize the configuration for DetrForObjectDetectionconfig = DetrConfig.from_pretrained("facebook/detr-resnet-50")# Create the model for object detection using the specified configurationmodel = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50", config=config)# Updated function callresults = processed_image(model, image, size={'longest_edge': 800})def get_pipeline_prediction(pil_image):# first get the pipeline output given the pil imagepipeline_output = od_pipe(pil_image)# Then Process the image using the pipeline outputprocessed_image = render_results_in_image(pil_image,pipeline_output)return processed_imagedemo = gr.Interface(fn=get_pipeline_prediction,inputs=gr.Image(label="Input image",type="pil"),outputs=gr.Image(label="Output image with predicted instances",type="pil"))demo.launch1234567891011121314151617181920212223242526from transformers import DetrImageProcessor, DetrForObjectDetectionimport torchfrom PIL import Imageimport requestsurl = "http://images.cocodataset.org/val2017/000000039769.jpg"image = Image.open(requests.get(url, stream=True).raw)# you can specify the revision tag if you don't want the timm dependencyprocessor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")inputs = processor(images=image, return_tensors="pt")outputs = model(**inputs)# convert outputs (bounding boxes and class logits) to COCO API# let's only keep detections with score > 0.9target_sizes = torch.tensor([image.size[::-1]])results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):box = [round(i, 2) for i in box.tolist()]print(f"Detected {model.config.id2label[label.item()]} with confidence "f"{round(score.item(), 3)} at location {box}")
app.py+21-28