I am reading frames using opencv and displaying it in browser but in order to avoid messy code I wrote my code in different files and importing them.
I wrote three files:
Flask_sh.py
open_webcam.py
show_gray.py
The open_webcam.py has a class Camera and a method get_frame.
open_webcam.py
class Camera():
def get_frame(self):
success, image = self.cap.read()
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
The show_gray.py also has a class named Calulate() and a method cal.
show_gray.py
class Calculate():
def cal(frame,self):
print(type(frame))
frame = imutils.resize(frame, width=640)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return gray
Now the Flask_sh.py is the file where I am calling both my files.
Flask_sh.py
from open_webcame import Camera
from show_gray import Calculate
while True:
camera = Camera()
frame = camera.get_frame()
decoded = cv2.imdecode(np.frombuffer(frame, np.uint8), -1) #convert from bytes to np array
cam1 = Calculate()
frame1 = cam1.cal(decoded)
cv2.imshow("Output", frame1)
Now I don't know where the issue is because get frame function is running fine and I am able to decode the image from bytes to array. But when I pass the decoded to cal method it gives me error in the first line iself as:
Error
frame = imutils.resize(frame, width=640)
AttributeError: 'show_gray' object has no attribute 'shape'
But when instead of making show_gray.py I write the code in Flask_sh.py I get the output. What am I doing wrong.