I’m pretty new to this so please bare with me, I’m using the roboflow library and cv2 library for my object detection model that I’m using with a docker with a raspberry pi, but I’m getting this error and trying to think what should be my next move. Anyone knowledgeable could be a great help since this is for my thesis study.
import cv2
import time
from picamera2 import Picamera2
from inference import InferencePipeline
from roboflow import Roboflow
import pandas as pd
import cvzone
import numpy as np
picam2a = Picamera2(0)
picam2b = Picamera2(1)
picam2a.preview_configuration.sensor.output_size = (640,480)
picam2b.preview_configuration.sensor.output_size = (640,480)
picam2a.preview_configuration.sensor.bit_depth = 10
picam2b.preview_configuration.sensor.bit_depth = 10
picam2a.preview_configuration.main.format = "RGB888"
picam2b.preview_configuration.main.format = "RGB888"
picam2a.preview_configuration.controls.FrameRate = 30
picam2b.preview_configuration.controls.FrameRate = 30
picam2a.configure("preview")
picam2b.configure("preview")
picam2a.start()
picam2b.start()
rf = Roboflow(api_key="~~~~~~~~~~~")
project = rf.workspace().project("~~~~~~~~~~~")
model = project.version(3, local="http:~~~~~~~~~~~~~/").model
classes = ['Ripe','Unripe','Overripe']
data = classes
class_list = data
count=0
fps = 0
while True:
tStart = time.time()
im= picam2a.capture_array()
im2= picam2b.capture_array()
count += 1
if count % 3 != 0:
continue
im=cv2.flip(im,0) # type: ignore
prediction=model.predict(im,confidence=40, overlap=30)
print(prediction.json())
a=prediction[0].boxdata
px=pd.DataFrame(a).astype("float")
for index,row in px.iterrows():
# print(row)
x1=int(row[0])
y1=int(row[1])
x2=int(row[2])
y2=int(row[3])
d=int(row[5])
c=class_list[d]
cv2.rectangle(im,(x1,y1),(x2,y2),(0,0,255),2)
cvzone.putTextRect(im,f'{c}',(x1,y1),1,1)
cv2.imshow("Camera1", im)
im2=cv2.flip(im2,0) # type: ignore
prediction=model.predict(im2, confidence=40, overlap=30)
print(prediction.json())
a=prediction[0].boxdata
px=pd.DataFrame(a).astype("float")
for index,row in px.iterrows():
# print(row)
x1=int(row[0])
y1=int(row[1])
x2=int(row[2])
y2=int(row[3])
d=int(row[5])
c=class_list[d]
cv2.rectangle(im2,(x1,y1),(x2,y2),(0,0,255),2)
cvzone.putTextRect(im2,f'{c}',(x1,y1),1,1)
cv2.imshow("Camera2", im2)
if cv2.waitKey(30)==ord('q'):
break
tEnd = time.time()
loopTime = tEnd-tStart
fps = .9*fps + .1*(1/loopTime)
print(int(fps))
cv2.destroyAllWindows()
This is the code I’ve been building and I’ve tested using YOLO before and it works just fine but I’ve had some trouble training my data with my local machine so I’ve transfered to the Roboflow docker instead and here’s the output error I’ve getting.
File "/home/ACSIPGuro/rpi-bookworm-yolov8/cam_3.py", line 53, in <module>
a=prediction[0].boxdata
~~~~~~~~~~^^^
File "/home/ACSIPGuro/.local/lib/python3.11/site-packages/roboflow/util/prediction.py", line 421, in __getitem__
return self.predictions[index]
~~~~~~~~~~~~~~~~^^^^^^^
IndexError: list index out of range
I’ve tried removing these lines and it works but the boxes that shows what it detects are not showing if I don’t define the box frames.
a=prediction[0].boxdata
px=pd.DataFrame(a).astype("float")
for index,row in px.iterrows():
# print(row)
x1=int(row[0])
y1=int(row[1])
x2=int(row[2])
y2=int(row[3])
d=int(row[5])
c=class_list[d]
cv2.rectangle(im2,(x1,y1),(x2,y2),(0,0,255),2)
cvzone.putTextRect(im2,f'{c}',(x1,y1),1,1)
Anyone have other ways of doing this or any other alternatives? Thanks in advance!
geekbird is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.