soham96/trash_classifier

Could not perform prediction without turning on the Picamera to start scanning

MeiSan0423 opened this issue · 7 comments

Dear Soham, as spoken in the LinkedIn, below is the problem I face:

  1. After running the code, the result shown 'Could not perform prediction' without turning on the Picamera to scan the object at the first place. I have attached a Picamera to the RPI and took an image with Picamera before running the code. Thus, seems the problem isn't from the camera.

Python Code that I am using: pi_trash_classifier.py
https://github.com/soham96/trash_classifier/blob/master/pi_trash_classifier.py

Things I have modified:
Change 'pic.png' to 'capture.jpg' in line 17 and 38.

Files that I included in the folder:

  1. model1.h5
  2. pi_trash_classifier.py
  3. An trash image named: capture.jpg

Below are the pictures to support my explanation [above:](url
553290F2-50A6-4417-82EA-71E772532BC7
)
6A97A35E-E677-48D6-A05A-FE2B3C3D1FF8

Hey MeiSan,

Thanks for the detailed description of the problem. So that is being caused because there is some error in the try block in the code snapshot that you have sent. Can you please try to make the following changes:

  1. Please remove the line import ipdb; ipdb.set_trace(). ipdb is a debugger and it will stop the flow of the execution. You can however, keep that line there and keep pressing n until you hit the line where the error is. That way it will be easier for you to debug.

  2. Have you installed the picamera and enabled the picamera? Please take a look at this article to see how to set up the picamera: https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

  3. You code for capturing the image does not look right. Ideally, it should be something like this:

from picamera.array import PiRGBArray
from picamera import PiCamera
 
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)
 
# grab an image from the camera
camera.capture(rawCapture, format="rgb")
image = rawCapture.array

You can take a look at the above article again to learn more about how to capture images with the picamera

Do let me know the results after you have done the above steps. thanks

Hi Soham, here is my update according to your previous comments.
I still unable to capture > detect > classify the trash even though I have completed the 3 steps you mentioned in your previous comments.

I have tried to troubleshoot the code by typing it into Raspberry PI Terminal line by line. No error pop up but it could not enter the 'try ' function. Keep return ' Could not perform detection'

Do you mind helping me to look at the code again to see if there any unforeseen mistake/ missing function in the code?

Here is your code and I have modified it based on your previous suggestion:
2019-10-10-235221_1280x1024_scrot
`
Appreciate for all the helps given!

Hey,

The code looks fine now. I am guessing it could be an issue with either some installation done incorrectly, for file permission issue.

Could you run the script as a whole and then send me the whole error output?

Hi Soham,

There is no error previously. I had tried typing out the code line by line into Raspberry Pi terminal, but it will still went into except loop: Could not perform prediction.

Yes, I suspected there might have things I left out so I have reinstalled everything according to your requirement.txt and do some changes in the codes, now it is able to detect and classify the object.

Thanks!
Just for sharing, here is part I modified from the original code: ( I removed try and except and other minor changes)

camera = PiCamera()
rawCapture=PiRGBArray(camera)

for i in range(5):
time.sleep(0.5)

# Access the image data.
camera.capture(rawCapture, format='rgb')
img=rawCapture.array
cv2.imwrite('pic.png', img)
pred_img=pp_image()
yo=model.predict(pred_img)
pred=prediction_list[np.argmax(yo)]
rawCapture.truncate(0)
print("Result of " + str(i) + ": ", pred)

camera.stop_preview()

And here is my result:
image

Awesome! So I hope everything is running as expected? If you get more data or make any more changes, I would love for you to contribute to the project!

Sure Soham. I will continue to update the technical parts if there is any interesting progress for my trash classifier project.

Btw, I would like to re-train the model.h5 as there are some items could not be classified accurately. Do you have any idea where can I get the existing code of this pre-train model?

Hello. Yes, you can re-train the model using the mobilenet_training script. You can get the data I used to train the model from the trashnet project. The details of the above are given in the readme. I am now closing this issue.