KeyError: '0' while using hops with python

Hello everyone! I’m new to Hops and Cpython and i was wondering if anyone can help me solve this issue. I’ve written a code to get the contour of an image using openCV but when i try to link to the hops component the file path to the image it doesn’t work. I’ll send the error under the code.

import sys
import os
import rhinoinside as rh
import cv2
# load ghhops-server-py source from this directory
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import ghhops_server as hs
import rhino3dm as dm

rh.load()
# System and Rhino can only be loaded after rhinoinside is initialized


hops = hs.Hops(app=rh)


@hops.component(
    "/list",
    name="get_object_contour",
    description="Get the contour of a object in a image",
    inputs=[
        hs.HopsString("File_path","Path","Connect the path to the image that you want to get the contour"),
    ],
    outputs=[
        #hs.HopsNumber("x_coordinate","x_coords","X coordinates of contour"),
        #hs.HopsNumber("y_coordinate","y_coords","Y coordinates of contour"),
        hs.HopsPoint("Contour_Points","Points","The contour points of the object"),
    ],
    )

def get_object_contour(file_path):
    # Load the image in grayscale
    img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)

    # Apply adaptive thresholding to create a binary image
    thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

    # Find the external contours in the binary image
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    # Get the x and y coordinates of the object contour
    x_coords = []
    y_coords = []
    
    for contour in contours:
        for point in contour:
            x_coords.append(point[0][0])
            y_coords.append(point[0][1])
    for i in range(len(x_coords)):
        points_3D = dm.Point3dList.Add(0,x_coords[i],y_coords[i])
    
                         
    return points_3D

if __name__ == "__main__":
    hops.start(debug=True)

[INFO] Starting hops python server on localhost:5000
[DEBUG] Getting component metadata: <HopsComponent /list [File_path → get_object_contour → Contour_Points] >
[DEBUG] True : {“Uri”: “/list”, “Name”: “get_object_contour”, “Nickname”: null, “Description”: “Get the contour of a object in a image”, “Category”: “Hops”, “Subcategory”: “Hops Python”, “Inputs”: [{“Name”: “File_path”, “Nickname”: “Path”, “Description”: “Connect the path to the image that you want to get the contour”, “ParamType”: “Text”, “ResultType”: “System.String”, “AtLeast”: 1, “AtMost”: 1, “Default”: “C:\Merda\AFS\u0001.png”}], “Outputs”: [{“Name”: “Contour_Points”, “Nickname”: “Points”, “Description”: “The contour points of the object”, “ParamType”: “Point”, “ResultType”: “Rhino.Geometry.Point3d”, “AtLeast”: 1, “AtMost”: 1}]}
[INFO] 127.0.0.1 - - [20/Mar/2023 15:53:06] “GET /list HTTP/1.1” 200 -
[DEBUG] Getting component metadata: <HopsComponent /list [File_path → get_object_contour → Contour_Points] >
[DEBUG] True : {“Uri”: “/list”, “Name”: “get_object_contour”, “Nickname”: null, “Description”: “Get the contour of a object in a image”, “Category”: “Hops”, “Subcategory”: “Hops Python”, “Inputs”: [{“Name”: “File_path”, “Nickname”: “Path”, “Description”: “Connect the path to the image that you want to get the contour”, “ParamType”: “Text”, “ResultType”: “System.String”, “AtLeast”: 1, “AtMost”: 1, “Default”: “C:\Merda\AFS\u0001.png”}], “Outputs”: [{“Name”: “Contour_Points”, “Nickname”: “Points”, “Description”: “The contour points of the object”, “ParamType”: “Point”, “ResultType”: “Rhino.Geometry.Point3d”, “AtLeast”: 1, “AtMost”: 1}]}
[INFO] 127.0.0.1 - - [20/Mar/2023 15:53:06] “GET /list HTTP/1.1” 200 -
[INFO] Solving using legacy API: <HopsComponent /list [File_path → get_object_contour → Contour_Points] >

Exception happened during processing of request from (‘127.0.0.1’, 53922)
Traceback (most recent call last):
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\socketserver.py”, line 683, in process_request_thread
self.finish_request(request, client_address)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\socketserver.py”, line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\middlewares\hopsdefault.py”, line 29, in init
super(_HopsHTTPHandler, self).init(request, client_address, server)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\socketserver.py”, line 747, in init
self.handle()
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\http\server.py”, line 427, in handle
self.handle_one_request()
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\http\server.py”, line 415, in handle_one_request
method()
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\middlewares\hopsdefault.py”, line 69, in do_POST
res, results = self.hops.solve(uri=comp_uri, payload=data)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\base.py”, line 126, in solve
return self._process_solve_request(comp, payload)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\base.py”, line 192, in _process_solve_request
res, inputs = self._prepare_inputs(comp, payload)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\base.py”, line 241, in _prepare_inputs
value = in_param.from_input(in_param_data)
File “C:\Users\paolo\AppData\Local\Programs\Python\Python38\lib\site-packages\ghhops_server\params.py”, line 209, in from_input
for param_value_item in input_data[“InnerTree”][“0”]:
KeyError: ‘0’

1 Like

I am facing the same error. How did you solve it?

I downgraded Hops to 0.15 and now it works

2 Likes

I didn’t find a way to solve it. I’ve found another way to connect a python script with grasshopper using GHowl

I’ll try to do it! Thanks!

I think we are working on a similar problem: running CV in GH. Would you like to chat on whatsapp? +4917681216305

same - using version 0.15.0