ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.75k stars 16.12k forks source link

I want to call detect method in"detect.py" from a Flask script. How to do that ? #221

Closed AshutoshAJ closed 4 years ago

github-actions[bot] commented 4 years ago

Hello @AshutoshAJ, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom model or data training question, please note that Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

For more information please visit https://www.ultralytics.com.

glenn-jocher commented 4 years ago

@AshutoshAJ I don't know!

If you figure it out a quick tutorial to help everyone else out might be really useful :)

AshutoshAJ commented 4 years ago

I'm trying @glenn-jocher But I'm a java guy. Completely new to python and facing a lot of difficulties in understanding the code.

pedromoraesh commented 4 years ago

@AshutoshAJ

You can put the arguments, used in parser.add_argument, in the Flask call. If you want to send image to server it can be done by using POST, if image is already in the server GET could be used to infer and then recieve information. So pass the parameters within the call.

Pure Flask you can put app.route("detect/") above the detect() method and then call. If you read the docs there is some explaination how to perform GETS, POST etc..

e.g: myapp:5000/detect?img=test.jpg&weights=yolov5x&...

I recommend Flask-Restplus, is a good wrapper for Flask with simplified and intuitive calls.
https://flask-restplus.readthedocs.io/en/stable/

glenn-jocher commented 4 years ago

@pedromoraesh @AshutoshAJ Flask seems like a common use case. It might be worth it to create a tutorial in the wiki on it's use or perhaps include a file in the repo to run a Flask demo. @pedromoraesh if you have extra time on your hands and would like to contribute, a PR would be great too :)

pedromoraesh commented 4 years ago

@glenn-jocher

i'm just about to finish some experiments in master program. I will try to code an example in the weekend!

As soon as I finish i will create a PR.

glenn-jocher commented 4 years ago

@pedromoraesh awesome! I don't actually know anything about Flask myself, but I've seen it pop up in a few different conversations. I'll be learning from the PR too then!

AshutoshAJ commented 4 years ago

Thanks @pedromoraesh . I'll just give it a go.

muhk01 commented 4 years ago

follow https://github.com/miguelgrinberg/flask-video-streaming at camera thread replace frames classes method with this

def frames(): out, weights, imgsz = \ 'inference/output', 'weights/yolov5s.pt', 640 source = '4.mp4' device = torch_utils.select_device() if os.path.exists(out): shutil.rmtree(out) # delete output folder os.makedirs(out) # make new output folder

    google_utils.attempt_download(weights)
    model = torch.load(weights, map_location=device)['model']

    model.to(device).eval()

    # Second-stage classifier
    classify = False
    if classify:
        modelc = torch_utils.load_classifier(name='resnet101', n=2)  # initialize
        modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model'])  # load weights
        modelc.to(device).eval()

    # Half precision
    half = False and device.type != 'cpu' 
    print('half = ' + str(half))
    '''
    print('augment = ' + str(opt.augment))
    print(opt.conf_thres)
    print(opt.iou_thres)
    print(opt.classes)
    print(opt.agnostic_nms)
    '''
    if half:
        model.half()

    # Set Dataloader
    vid_path, vid_writer = None, None
    dataset = LoadImages(source, img_size=imgsz)
    #dataset = LoadStreams(source, img_size=imgsz)
    names = model.names if hasattr(model, 'names') else model.modules.names
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]

    # Run inference
    t0 = time.time()
    classSend = []
    countSend = []
    img = torch.zeros((1, 3, imgsz, imgsz), device=device)  # init img
    _ = model(img.half() if half else img) if device.type != 'cpu' else None  # run once
    for path, img, im0s, vid_cap in dataset:
        img = torch.from_numpy(img).to(device)
        img = img.half() if half else img.float()  # uint8 to fp16/32
        img /= 255.0  # 0 - 255 to 0.0 - 1.0
        if img.ndimension() == 3:
            img = img.unsqueeze(0)

        # Inference
        t1 = torch_utils.time_synchronized()
        pred = model(img, augment=False)[0]

        # Apply NMS
        pred = non_max_suppression(pred, 0.4, 0.5,
                           fast=True, classes=None, agnostic=False)
        t2 = torch_utils.time_synchronized()

        # Apply Classifier
        if classify:
            pred = apply_classifier(pred, modelc, img, im0s)

        for i, det in enumerate(pred):  # detections per image
            p, s, im0 = path, '', im0s

            save_path = str(Path(out) / Path(p).name)
            s += '%gx%g ' % img.shape[2:]  # print string
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  #  normalization gain whwh
            if det is not None and len(det):
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += '%g %s, ' % (n, names[int(c)])  # add to string
                    listDet = ['person','bicycle','car','motorbike','bus','truck','bird','cat','dog','horse','cow','backpack','umbrella','handbag','kite','cell phone']

                    if(str(names[int(c)]) in listDet):
                        countSend.append('%s' % (names[int(c)]))
                        classSend.append('%g' % (n))

                for *xyxy, conf, cls in det:
                    label = '%s %.2f' % (names[int(cls)], conf)
                    plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

        yield cv2.imencode('.jpg', im0)[1].tobytes()

i was successfully run yolov5 on flask framework, or checkout my repo

yasersakkaf commented 4 years ago

I have implemented Yolov5 as an API. I have also opened a PR for the same. Please check my repo if you want the implementation: https://github.com/yasersakkaf/yolov5

shindesud commented 3 years ago

I have implemented Yolov5 as an API. I have also opened a PR for the same. Please check my repo if you want the implementation: https://github.com/yasersakkaf/yolov5

Hello yasersakkaf, I want to improve detection speed for image. I don't know how to do that. Can you please suggest some ways to reduce detection time for detect method. Thanks.

glenn-jocher commented 3 years ago

YOLOv5 Flask API demo code is here: https://github.com/ultralytics/yolov5/tree/master/utils/flask_rest_api