Getting a 401 error when doing inference

Hello,

I am getting a 401 error when doing inference, and I am not sure why. I am using rock-paper-scissors-sxsw/11 as my model. This is my code:


def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

build = []
url_base = 'https://detect.roboflow.com/'
endpoint = os.getenv('ROBOFLOW_URL')
access_token = os.getenv('ROBOFLOW_API_KEY')
format = '&format=json'
confidence = '&confidence=35'
stroke = '&stroke=3'
overlap = '&overlap=50'
build.append(url_base)
build.append(endpoint)
build.append(access_token)
build.append(format)
build.append(confidence)
build.append(overlap)
build.append(stroke)
url = ''.join(build)
name = "picture" + str(time.time()) + ".png"
my_screenshot = pyautogui.screenshot()
my_screenshot.save(name)
f = name
# Creating the kernel(2d convolution matrix)
kernel = np.array([
    [0, -1, 0],
    [-1, 5, -1],
    [0, -1, 0]
])
image = cv2.imread(f)
# Applying the filter2D() function
img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel)
clean = cv2.imwrite(name, img)
image = Image.open(name).convert("RGB")
# Convert to JPEG Buffer
buffered = io.BytesIO()
image.save(buffered, quality=90, format="JPEG")
# Construct the URL
m = MultipartEncoder(fields={'file': (f, buffered.getvalue(), resource_path("image/jpeg"))})
r = requests.post(url, data=m, headers={'Content-Type': m.content_type})

Hi! It appears you are not adding a ?api_key= element. My advice is to use our native SDK, which will avoid these types of errors.

rf = Roboflow(api_key="API_KEY")
project = rf.workspace().project("MODEL_ENDPOINT")
model = project.version(VERSION).model

# infer on a local image
print(model.predict("your_image.jpg", confidence=40, overlap=30).json())

# visualize your prediction
# model.predict("your_image.jpg", confidence=40, overlap=30).save("prediction.jpg")

# infer on an image hosted elsewhere
# print(model.predict("URL_OF_YOUR_IMAGE", hosted=True, confidence=40, overlap=30).json())```

Thank you, but quick question. How does this code work when you are trying to infer from a model that is not yours but is on the roboflow universe?

If you go to the universe page for the model and scroll down, you’ll see code to deploy.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.