0% found this document useful (0 votes)
59 views

Test Programs1

This document contains code for a Kivy application that uses OpenCV to capture video from a webcam and display the frames in a Kivy widget. It defines a CamApp class that inherits from the Kivy App class. The CamApp builds a vertical BoxLayout with an Image widget to display the frames. It initializes the OpenCV video capture, schedules an update function to read frames and update the texture every 33 milliseconds. The update function reads frames, converts to texture and updates the Image widget.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Test Programs1

This document contains code for a Kivy application that uses OpenCV to capture video from a webcam and display the frames in a Kivy widget. It defines a CamApp class that inherits from the Kivy App class. The CamApp builds a vertical BoxLayout with an Image widget to display the frames. It initializes the OpenCV video capture, schedules an update function to read frames and update the texture every 33 milliseconds. The update function reads frames, converts to texture and updates the Image widget.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

__author__ = 'bunkus'

from kivy.app import App


from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.lang import Builder
import requests
import bs4 as Beautifulsoup
import cv2

class CamApp(App):
def build(self, layout ): # pass on the missing layout
self.img1=Image()
layout = BoxLayout()
layout = BoxLayout(('''
orientation:'vertical'
BoxLayout:
orientation:'vertical'
Button:
background_color(0,0,0,0)
background_normal:
text:'Capture'
canvas.before:
Color:
rgba:(30/255, 144/255, 1, 1)
RoundRectangle:
pos:self.pos
size:self.size
radius:[48]
on_press: print("Bye!")

'''))
layout.add_widget(self.img1)
#opencv2 stuffs
self.capture = cv2.VideoCapture(0)
cv2.namedWindow("CV2 Image")
Clock.schedule_interval(self.update, 1.0/33.0)
return layout

def update(self, dt):


# display image from cam in opencv window
ret, frame = self.capture.read()
cv2.imshow("CV2 Image", frame)
# convert it to texture
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]),
colorfmt='bgr')
#if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick
with "bgr" in blit_buffer.
texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
# display image from the texture
self.img1.texture = texture1

if __name__ == '__main__':
CamApp().run()
cv2.destroyAllWindows()
______________________________________________________________________-------------
-----------------------------------------------------------------------------------
-------------------------------------------------------------------

from kivy.app import App


from kivy.metrics import dp
from kivy.uix.behaviors import TouchRippleBehavior
from kivy.uix.button import Button
from kivy.lang import Builder

KV = """
<RectangleFlatButton>:
ripple_color: 0, 0, 0, .2
background_color: 0, 0, 0, 0
color: root.primary_color
canvas.before:
Color:
rgba: root.primary_color
Line:
width: 1
rectangle: (275, 100, 240, 30)
Screen:
canvas:
Color:
rgb: 181, 2, 1
Rectangle:
pos: 275, 100
size: 240, 30
"""

class RectangleFlatButton(TouchRippleBehavior, Button):


primary_color = [
0.12941176470588237,
0.5882352941176471,
0.9529411764705882,
1
]

def on_touch_down(self, touch):


collide_point = self.collide_point(touch.x, touch.y)
if collide_point:
touch.grab(self)
self.ripple_show(touch)
return True
return False

def on_touch_up(self, touch):


if touch.grab_current is self:
touch.ungrab(self)
self.ripple_fade()
return True
return False

class MainApp(App):
def build(self):
screen = Builder.load_string(KV)
screen.add_widget(
RectangleFlatButton(
text="Select an Image",
pos_hint={"center_x": 0.5, "center_y": 0.1919},
size_hint=(None, None),
size=(dp(110), dp(35)),
ripple_color=(0.8, 0.8, 0.8, 0.5),
)
)
return screen

You might also like