I'm beginner in Kivy, I make a screen which has an image in which a camera live feed is fitted and 2 buttons the 1st start camera which open the webcam and the 2nd is the icon button to take a picture and store it locally but the problem is that if i clicked the icon button before clicking the start camera it give me this error.
cv2.imwrite(image_name, self.image_frame)
AttributeError: 'WebCamScreen' object has no attribute 'image_frame'
So , how should i put a dialogbox or some other condition to tell the user to start camera first.
Here is my WebCamScreen
code
class WebCamScreen(Screen):
def do_start(self):
self.capture = cv2.VideoCapture(0)
Clock.schedule_interval(self.load_video, 1.0 / 24.0)
def load_video(self, *args):
ret, frame = self.capture.read()
self.image_frame = frame
# frame = frame[220:220 250, 400:400 250, :]
buffer = cv2.flip(frame, 0).tostring()
image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt="bgr")
image_texture.blit_buffer(buffer, colorfmt="bgr", bufferfmt="ubyte")
self.ids.img.texture = image_texture
def capture_image(self):
image_name = "first_pic.jpg"
cv2.imwrite(image_name, self.image_frame)
and kv
code is below
<WebCamScreen>
MDFloatLayout:
MDRaisedButton:
text: "Start Camera"
size_hint_x: None
size_hint_y: None
md_bg_color: "orange"
pos_hint: {"center_x": 0.5, "center_y": 0.95}
on_release:
root.do_start()
Image:
id: img
size_hint_x: 0.85
size_hint_y: 0.5
pos_hint: {"center_x": 0.5, "center_y": 0.6}
MDIconButton:
icon: "camera"
md_bg_color: "orange"
pos_hint: {"center_x": .5, "center_y": .2}
on_release:
root.capture_image()
CodePudding user response:
You can put that line that causes the exception in a try
block. And in the except
block display a Popup
.
Or you can disable
the capture_image
Button, and enable it from within the load_video()
method.