Raspberry Pi becomes unresponsive when using OpenCV

I'm using Raspberry Pi 4B and running OpenCV on it. I have attached webcam for video steam to detect objects using OpenCV, also, a mouse and keyboard. I'm facing a very unique problem here, when I run the python script containing the OpenCV program, it runs smoothly without any errors, and the frame window (webcam) is opened for detection. The streaming program continues to run fine, but after some time (say 50 frames), the RGB lights on my mouse and keyboard turns off meaning that they stop working (goes blank) as well as the webcam which is also connected to one of the USB ports, becomes unresponsive, but everything turns back on after a second or two. So, the whole problem process takes like 1~2 seconds. Due to this problem, the program shows an error.

Error:

frame = imutils.resize(frame, width=400)
File "home\pi\python\lib\site-packages\imutils\convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'


which basically means that the program is unable to read frames from the webcam as it gets non-responsive for like 1~2 seconds. Not to mention that this problem doesn't occur while idling or doing anything else.

What could be the cause that is making Raspberry Pi do that weird thing?

PS: I have run this program on Windows PC multiple times and it runs smoothly without giving any errors.