My knowledge of Python feels very weak atm., but I'm essentially creating a project with a group of 4 others, wherein we're performing object detection. My task is to save cropped images containing the detected objects and save them to a directory. However when I test the saving function, if I add a print statement to the very top of the definition of the function, I don't see the print statement when attempting to run the function, making me very confused π₯²
#π When debugging my code, the specific function I wish to debug seemingly doesn't get reached?
74 messages Β· Page 1 of 1 (latest)
@fiery seal
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
It's a little hard to show all the relevant code in one screenshot, but the save_cropped_blobs function is the one that seemingly doesn't run, and it isn't giving me any errors:
can't help without all the code. plus detailed instructions on how you're trying to run it
but sticking the print in there is a good idea
!code
Yeah fair
does it exit in one of the functions before?
if the file is too big for discord you can paste it here
!paste
Pasting large amounts of code
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
Hey @fiery seal!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
Okay so save_cropped_blobs function looks like this
def save_cropped_blobs(input_image, bounding_boxes):
save_dir = "Datasets/Patches"
os.makedirs(save_dir, exist_ok=True)
print(f"Saving images to: {os.path.abspath(save_dir)}")
blob_data = []
for i, (x, y, w, h) in enumerate(bounding_boxes):
# Crop the region based on bounding box dimensions
blob_region = input_image[y:y + h, x:x + w]
# Store blob data in the list
blob_data.append({"id": i, "region": blob_region})
# Save the cropped blob region as an image file
filename = os.path.join(save_dir, f"blob_{i}.jpg")
cv.imwrite(filename, blob_region)
return blob_data
This is inside a separate script which when I think about it, it doesn't need to be at all π
def save_cropped_blobs(input_image, bounding_boxes):
save_dir = "Datasets/Patches"
os.makedirs(save_dir, exist_ok=True)
print(f"Saving images to: {os.path.abspath(save_dir)}")
blob_data = []
for i, (x, y, w, h) in enumerate(bounding_boxes):
# Crop the region based on bounding box dimensions
blob_region = input_image[y:y + h, x:x + w]
# Store blob data in the list
blob_data.append({"id": i, "region": blob_region})
# Save the cropped blob region as an image file
filename = os.path.join(save_dir, f"blob{i}.jpg")
cv.imwrite(filename, blob_region)
return blob_data```
Ty π₯²
does it even reach this function?
can you try to print("here") in your main.py just before you call this function
Yeah it doesn't I believe
try the print thing
move the print up by 1 function
so the problem must be inside the classify_human function
Should be in here, I'll post it here with the py syntax rq
Hey @fiery seal!
It looks like you are trying to paste code into this channel.
You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not ´´´.
Here is an example of how it should look:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
def classify_human(masked_image):
# Find contours of the blobs in the masked image
contours, _ = cv.findContours(masked_image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
human_detected = False
bounding_boxes = [] # New: List to store dimensions
for contour in contours:
# Calculate area and bounding box of each blob
area = cv.contourArea(contour)
x, y, w, h = cv.boundingRect(contour)
# Print out the blob properties for debugging
print(f"Blob at ({x}, {y}) with width={w}, height={h}, area={area}")
# Test with a very low area threshold for classification
if area > 150: # Low threshold
human_detected = True
# Draw a bounding box and label as "Human" for blobs that meet the criteria listed
cv.rectangle(masked_image, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv.putText(masked_image, "Human", (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
bounding_boxes.append((x, y, w, h)) # New: Appends the dimensions of the bounding boxes to the list
else:
# Label as "Not Human" if the blob does not meet the criteria listed
cv.rectangle(masked_image, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv.putText(masked_image, "Not Human", (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
print(f"Detected humans: {human_detected}, Bounding boxes: {bounding_boxes}")
# Show the classified image
cv.imshow("Human Classification", masked_image)
cv.waitKey(0)
cv.destroyAllWindows()
return human_detected, bounding_boxes # New: Also returns bounding boxes
print(f"Detected humans: {human_detected}, Bounding boxes: {bounding_boxes}")```
we can see from the output that this line gets executed
Yes
so the problem must be below this line
Yeah I see a cv.destroyAllWindows() which almost sounds like exit..?
# Show the classified image
cv.imshow("Human Classification", masked_image)
cv.waitKey(0)
cv.destroyAllWindows()
return human_detected, bounding_boxes # New: Also returns bounding boxes```
so inside here
i never used cv before so idk
try it out
Hmm nah didn't fix it, moving the print statement back to before the save function, didnt make the print work
does it actually exit or is it stuck?
After deleting destroywindows
Mmmm I'm not sure about that tbh
Nah not without me having to click exit
Process finished with exit code 0 this should be printed at the bottom
cv.waitKey(0)```
is it expecting an input here?
to my knowledge that doesn't expect an input
I'll try removing it and see if that does anything though
Ding ding ding
π
Saved the blobs properly and it reached the lower print statements
Really appreciate you taking time out of your day to help me troubleshoot my code π₯²
yeah no problem
pycharm has a really good debugger
you should use it
its that bug icon next to the run button
you can set points in your code by clicking on the line number
if you run your code you can press f8 to go to the next line or you can press f9 to go to the next breakpoint
using this in combination with print("here") makes debugging very easy
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.