Skip to content

Commit

Permalink
change some features of main python file
Browse files Browse the repository at this point in the history
  • Loading branch information
vijethph authored May 13, 2020
1 parent 28ddd53 commit 2337c71
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions Python/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Kivy OpenCV Barcode Scanner
# done by Vijeth P H
# done by Vijeth P H
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.label import Label
Expand All @@ -10,36 +11,36 @@
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from pyzbar import pyzbar
import webbrowser
import cv2

# Create global variables, for storing and displaying barcodes
outputtext=''
weblink=''
leb=Label(text=outputtext,size_hint_y=None,height='48dp',font_size='45dp')
found = set() # this will not allow duplicate barcode scans to be stored
togglflag=True

class BarcodeApp(App):
# initialize all widgets for display
def build(self):
layout=BoxLayout() # boxlayout is where widgets are placed adjacent to each other
layout.orientation='vertical' # make adjacency of widgets vertical
# create OpenCV camera
self.cam=cv2.VideoCapture(0)
class MainScreen(BoxLayout):
# first screen that is displayed when program is run
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.orientation='vertical' # vertical placing of widgets

self.cam=cv2.VideoCapture(0) # start OpenCV camera
self.cam.set(3,1280) # set resolution of camera
self.cam.set(4,720)
self.img=Image()
self.img=Image() # Image widget to display frames

# create Toggle Button for pause and play of video stream
self.togbut=ToggleButton(text='Pause',group='camstart',state='down',size_hint_y=None,height='48dp',on_press=self.change_state)
self.but=Button(text='Stop',size_hint_y=None,height='48dp',on_press=self.stop_stream)
layout.add_widget(self.img)
layout.add_widget(leb)
layout.add_widget(self.togbut)
layout.add_widget(self.but)
self.add_widget(self.img)
self.add_widget(self.togbut)
self.add_widget(self.but)
Clock.schedule_interval(self.update,1.0/30) # update for 30fps

return layout


# update frame of OpenCV camera
def update(self,dt):
if togglflag:
Expand All @@ -58,16 +59,19 @@ def update(self,dt):
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
weblink=barcodeData
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(frame, text, (x, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if barcodeData not in found: # check if detected barcode is a duplicate
outputtext=text
leb.text=outputtext # display the barcode details
found.add(barcodeData)
self.change_screen()

key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
pass
cv2.destroyAllWindows()
exit(0)

# change state of toggle button
def change_state(self,*args):
Expand All @@ -81,9 +85,40 @@ def change_state(self,*args):


def stop_stream(self,*args):
self.cam.release()


self.cam.release() # stop camera

def change_screen(self,*args):
main_app.sm.current='second' # once barcode is detected, switch to second screen

class SecondScreen(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.orientation='vertical'
self.lab1=Label(text='Output: ',size_hint_y=None,height='48dp',font_size='45dp')
self.but1=Button(text='Open in Web Browser',on_press=self.open_browser,size_hint_y=None,height='48dp')
self.add_widget(self.lab1)
self.add_widget(leb)
self.add_widget(self.but1)

def open_browser(self,*args):
webbrowser.open(weblink) # this opens link in browser

class TestApp(App):
def build(self):
self.sm=ScreenManager() # screenmanager is used to manage screens
self.mainsc=MainScreen()
scrn=Screen(name='main')
scrn.add_widget(self.mainsc)
self.sm.add_widget(scrn)

self.secondsc=SecondScreen()
scrn=Screen(name='second')
scrn.add_widget(self.secondsc)
self.sm.add_widget(scrn)

return self.sm

if __name__ == '__main__':
BarcodeApp().run()
cv2.destroyAllWindows()
main_app=TestApp()
main_app.run()
cv2.destroyAllWindows()

0 comments on commit 2337c71

Please sign in to comment.