Infer() returns None, Webcam API

ROBOFLOW_API_KEY = "*******"
ROBOFLOW_MODEL = "rummy-tiles-detector/2" # eg xx-xxxx--#

import cv2
import base64
import numpy as np
import requests

upload_url = "".join([

video = cv2.VideoCapture(1)
# Infer via the Roboflow Infer API and return the result
def infer():
    # Get the current image from the webcam
    ret, img =

    # Resize (while maintaining the aspect ratio) to improve speed and save bandwidth
    height, width, channels = img.shape
    scale = ROBOFLOW_SIZE / max(height, width)
    img = cv2.resize(img, (round(scale * width), round(scale * height)))

    h,w,_ = img.shape

    # Encode image to base64 string
    retval, buffer = cv2.imencode('.jpg', img)
    img_str = base64.b64encode(buffer)

    # Get prediction from Roboflow Infer API
    resp =, data=img_str, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    }, stream=True).raw
    # Parse result image
    image = np.asarray(bytearray(, dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    return image

 # Main loop; infers sequentially until you press "q"
while 1:
    # On "q" keypress, exit
    if(cv2.waitKey(1) == ord('q')):

    # Synchronously get a prediction from the Roboflow Infer API
    image = infer()
    # And display the inference results
    cv2.imshow('image', image)


On this code I provided, infer() returns None for some reason. When I print the infer() function it says None. I believe the issue there is on resp = part. I checked all the previous steps and they are doing fine. Can someone help?

Add “&confidence=20” as a parameter and try again. You might not be detecting anything. The default confidence it predicts at is 40%.

It does not work when I set confidence 0 either. I feel like there’s something else. The model works perfectly when I try it on roboflows webcam website that shows up after training. Also why would it return None if there is nothing detected on webcam? It’s a real-time video and it shouldn’t just throw an error if there is nothing detected. Just like the one on the website