Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines789101112131415161718192021222324252627282930101112136 unmodified linesfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictorimport torchvision
# Path to the local model fileLOCAL_MODEL_PATH = "faster_rcnn_state.pth"
# Load modelif os.path.exists(LOCAL_MODEL_PATH): # Create model model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=None, weights_backbone=None) # Update the box predictor to match the number of classes in the pretrained weights (21) num_classes = 21 # 20 classes + background in_features = model.roi_heads.box_predictor.cls_score.in_features model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes) # Load weights model.load_state_dict(torch.load(LOCAL_MODEL_PATH, map_location='cpu')) model.eval() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device)else: print("Failed to load model")
# Dictionary for class labelsclasses = {0: 'background', 1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat', 5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair', 10: 'cow', 11: 'diningtable', 12: 'dog', 13: 'horse', 14: 'motorbike', 15: 'person', 16: 'pottedplant', 17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor'}# ... (keep the model loading code as it was) ...
def obj_detector(image): # Convert image to RGBapp.py+1-21