use try except block even if the image is missing, the program should not crash: class…
- User
use try except block even if the image is missing, the program should not crash: class inferThread(threading.Thread): def __init__(self, yolov7_wrapper): threading.Thread.__init__(self) self.yolov7_wrapper = yolov7_wrapper self.cap = cv2.VideoCapture('rtsp://admin:Dats$123@192.168.2.241/Streaming/Channels/101/') self.fps=0 #self.YoLov7TRT=YoLov7TRT
HH
def run(self): while(True): ret, frame = self.cap.read() start_time=time.time() ################# img = cv2.resize(frame, (640, 480)) ###[:,240:1680] result, use_time,result_classid= self.yolov7_wrapper.infer(img) self.fps = 1.0 / (time.time() - start_time) cv2.imshow("Recognition result", result) #####################
img_class = self.yolo7_trt.img_class
img_class = self.YoLov7TRT.img_class
#if YoLov7TRT.non_max_suppression.img_class==1: if 1 in result_classid:
#if self.YoLov7TRT.img_class == 1:
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
_, compressed_data = cv2.imencode('.jpg', frame1, encode_param)
# Write compressed image to file
with open(r"Path/images_data/Helmet_%s.jpg"%(time.time()), 'wb') as f:
f.write(compressed_data)
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f') filename = f"frame_{timestamp}.jpg"
Set the full output path by joining the output directory path and the filename
full_out_path = os.path.join(out_path, filename) cv2.imwrite(full_out_path, frame) ########### #cv2.imshow("Recognition result depth",depth_colormap) if cv2.waitKey(1) & 0xFF == ord('q'): breakShare Prompt
- Assistant
To handle missing image exceptions and prevent program crashes, you can use a try-except block to wrap the cv2.imwrite() function call. Here's an example of how you can modify your code to handle missing images:
python - User
rtsp link crashes the program.use try except for rtsp streamShare Prompt
- Assistant
To handle exceptions that might be raised when trying to open an RTSP stream, you can use a try-except block to wrap the
cv2.VideoCapture()function call. Here's an example of how you can modify your code to handle RTSP stream exceptions:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.