CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AmitNikhade

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: AmitNikhade/Opencv-Kivy
Path: blob/main/main.py
Views: 117
1
from kivy.app import App
2
from kivy.uix.widget import Widget
3
from kivy.uix.boxlayout import BoxLayout
4
from kivy.uix.image import Image
5
from kivy.clock import Clock
6
from kivy.graphics.texture import Texture
7
8
import cv2
9
10
class CamApp(App):
11
12
def build(self):
13
self.img1=Image()
14
layout = BoxLayout()
15
layout.add_widget(self.img1)
16
#opencv2 stuffs
17
self.capture = cv2.VideoCapture(0)
18
cv2.namedWindow("CV2 Image")
19
Clock.schedule_interval(self.update, 1.0/33.0)
20
return layout
21
22
def update(self, dt):
23
# display image from cam in opencv window
24
ret, frame = self.capture.read()
25
cv2.imshow("CV2 Image", frame)
26
# convert it to texture
27
buf1 = cv2.flip(frame, 0)
28
buf = buf1.tostring()
29
texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
30
#if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with "bgr" in blit_buffer.
31
texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
32
# display image from the texture
33
self.img1.texture = texture1
34
35
if __name__ == '__main__':
36
CamApp().run()
37
cv2.destroyAllWindows()
38
39