Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emergent Object Detection/Classification Model.v1 #56

Closed
wants to merge 8 commits into from
Binary file added vision/emergent_object/best.pt
Binary file not shown.
61 changes: 61 additions & 0 deletions vision/emergent_object/emergent_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import torch

# You may have to install:
# pandas
# torchvision
# tqdm
# seaborn
Comment on lines +5 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies should all be taken care of by #58 if you run in a poetry shell. These comments can be removed.


MODEL_PATH = "vision/emergent_object/best.pt"
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this constant is only used in emergent_object_model(), constant MODEL_PATH should be moved to that function.



# Function to do detection / classification
def emergent_object_detection(image, model):
# Convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hint for image should be Image type alias from vision > common > constants.py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assigning the result of cv2.cvtColor to the parameter image, assign it to a new variable (i.e. image_rgb)


# Run the model on the image. Both a file path and a numpy image work, but
# we want to use a numpy image
results = model(image)

# Retrieve the output from the model
output = results.pandas().xyxy[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type hints for results and output

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

results and output aren't very descriptive variable names


return output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function needs:

  • docstring
  • parameter types
  • return type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the docstring for this function, make sure to describe what kind of output one should expect from it.



# Load the model from the file
def emergent_object_model():
model = torch.hub.load("ultralytics/yolov5", "custom", path=MODEL_PATH)
return model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function needs:

  • docstring
  • return type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need type hint for model



if __name__ == "__main__":
import cv2

Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cv2 import should be at the top of the file since it is used in emergent_object_detection()

image_path = "vision/emergent_object/people.png"

Comment on lines +67 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could make this into a command line argument

image = cv2.imread(image_path)

# Create model
model = emergent_object_model()

# Use model for detection / classification
output = emergent_object_detection(image, model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hints for image, model, output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output isn't a very descriptive variable name


# Convert the Pandas Dataframe to a dictionary - this will be necessary and
# should eventually be done in `emergent_object_detection()`
output_dict = output.to_dict("index")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should do what this comment says about the emergent_object_detection function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type hint for output_dict


# Draw the bounding boxes to the original image
for row in output_dict.values():
Comment on lines +81 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hint for row. Loop variables are type hinted like this:

row: Type
for row ...

# Get the output ranges
top_left = (int(row["xmin"]), int(row["ymin"]))
bottom_right = (int(row["xmax"]), int(row["ymax"]))
Comment on lines +83 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type hints for these two variables


# Draw the bounding box
cv2.rectangle(image, top_left, bottom_right, (255, 0, 0), 4)

# Display the image
cv2.imshow("", image)
Comment on lines +90 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want something like "Emergent Object" instead of an empty string for the title.

cv2.waitKey(0)