Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Shuffled Camera Feed Puzzle
- ===========================
- This demonstrates using Scatter widgets with a live camera.
- You should see a shuffled grid of rectangles that make up the
- camera feed. You can drag the squares around to see the
- unscrambled camera feed or double click to scramble the grid
- again.
- '''
- import os
- import time
- from sys import stderr
- import cv2
- import numpy as np
- from kivy import Logger
- from kivy.app import App
- from kivy.app import dirname
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.button import Button
- from kivy.uix.camera import Camera
- from kivy.uix.image import Image
- from kivy.uix.textinput import TextInput
- from kivy.uix.screenmanager import Screen, ScreenManager
- from kivy.utils import platform
- from kivy.uix.widget import Widget
- from kivy.uix.slider import Slider
- from kivy.uix.scatter import Scatter
- from kivy.animation import Animation
- from kivy.graphics import Color, Rectangle
- from kivy.properties import NumericProperty
- from random import randint, random
- from functools import partial
- from client import *
- DIR = ''
- class MainScreen(Screen):
- def __init__(self, **kwargs):
- super(MainScreen, self).__init__(**kwargs)
- self.path_to_src = App.get_running_app().storage #os.path.join('/sdcard', 'DCIM') if platform == 'android' else ''
- self.path_to_save = os.path.join(self.path_to_src, 'res')
- self.path_to_styles = os.path.join(self.path_to_src, 'styles')
- # self.log_file_path = os.path.join(self.path_to_src, 'log.txt')
- # fl = open(self.log_file_path, 'a+')
- # fl.write('\n\n\nNEW SESS--------------------------------')
- self.camera = Camera(resolution=(640, 480), play=True)
- # fl.write("\n+ successfuly set camera")
- stderr.write("STYLETRANSFER = successfuly set camera")
- Logger.critical("STYLETRANSFER = {}".format("successfuly set camera"))
- self.filters = ['mosaic', 'wave']
- # fl.write("\n+ successfuly set filters names")
- stderr.write("STYLETRANSFER = successfuly set filters names")
- Logger.critical("STYLETRANSFER = {}".format("successfuly set filters names"))
- self.cur_filter_id = 0
- # fl.write("\n+ successfuly set cur filter id")
- stderr.write("STYLETRANSFER = successfuly set cur filter id")
- Logger.critical("STYLETRANSFER = {}".format("successfuly set cur filter id"))
- self.style_img = Image(source=os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id])), size=(640, 480))
- # fl.write("\n+ successfuly set style image source")
- stderr.write("STYLETRANSFER = successfuly set style image source")
- Logger.critical("STYLETRANSFER = {}".format("successfuly set style image source {}".format(os.path.abspath(self.style_img.source))))
- self.result_img = Image(source="2.jpeg", size=(640, 480))
- # fl.write("\n+ successfuly set results image source")
- stderr.write("STYLETRANSFER = successfuly set results image source")
- Logger.critical("STYLETRANSFER = {}".format(
- "successfuly set result image source {}".format(os.path.abspath(self.result_img.source))))
- # fl.write("\n...creating main layout")
- stderr.write("STYLETRANSFER = ...creating main layout")
- Logger.critical("STYLETRANSFER = {}".format("...creating main layout"))
- main_layout = BoxLayout(orientation='vertical')
- ngrok_addr = TextInput(font_size='20sp', height=30, size_hint=(None, None))
- main_layout.add_widget(ngrok_addr)
- # fl.write("\n......creating img layout")
- stderr.write("STYLETRANSFER = ...creating img layout")
- Logger.critical("STYLETRANSFER = {}".format("...creating img layout"))
- img_layout = BoxLayout(orientation='horizontal')
- img_layout.add_widget(self.camera)
- # fl.write("\n...... + successfully add camera to img layout")
- stderr.write("STYLETRANSFER = ...successfully add camera to img layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add camera to img layout"))
- img_layout.add_widget(self.style_img)
- # fl.write("\n...... + successfully add style img to img layout")
- stderr.write("STYLETRANSFER = ...successfully add style img to img layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add style img to img layout"))
- img_layout.add_widget(self.result_img)
- # fl.write("\n...... + successfully add res img to img layout")
- stderr.write("STYLETRANSFER = ...successfully add res img to img layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add res img to img layout"))
- main_layout.add_widget(img_layout)
- # fl.write("\n... + successfuly add img layout to main layout")
- stderr.write("STYLETRANSFER = ...successfuly add img layout to main layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add img layout to main layout"))
- def change_filter(instance):
- # fl = open(self.log_file_path, 'a+')
- # fl.write("\nCHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters)))
- stderr.write("STYLETRANSFER = CHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters)))
- Logger.critical("STYLETRANSFER = {}".format("\nCHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters))))
- self.cur_filter_id = (self.cur_filter_id + 1) % len(self.filters)
- print('cur id: {}'.format(self.cur_filter_id))
- print("{}.jpg".format(self.filters[self.cur_filter_id]))
- # fl.write("\nnew style img path: {}".format(os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id]))))
- stderr.write("STYLETRANSFER = new style img path: {}".format(os.path.abspath(os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id])))))
- Logger.critical("STYLETRANSFER = {}".format("new style img path: {}".format(os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id])))))
- self.style_img.source = os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id]))
- # fl.write("\n+ successfully changed source")
- stderr.write("STYLETRANSFER = successfully changed source")
- Logger.critical("STYLETRANSFER = {}".format("successfully changed source"))
- self.style_img.reload()
- # fl.write("\n+ successfully reloaded")
- stderr.write("STYLETRANSFER = successfully reloaded")
- Logger.critical("STYLETRANSFER = {}".format("successfully reloaded"))
- # fl.close()
- def apply_filter(instance):
- # fl = open(self.log_file_path, 'a+')
- # fl.write("\nAPPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id]))
- stderr.write("STYLETRANSFER = APPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id]))
- Logger.critical("STYLETRANSFER = {}".format("APPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id])))
- timestr = time.strftime("%Y%m%d_%H%M%S")
- im_path = "IMG_{}.png".format(timestr)
- path = os.path.join(self.path_to_save, im_path)
- # fl.write("\nimg path: {}".format(path))
- stderr.write("STYLETRANSFER = img path: {}".format(os.path.abspath(path)))
- Logger.critical("STYLETRANSFER = {}".format("img path: {}".format(path)))
- self.camera.export_to_png(path)
- # fl.write("\n+ successfully exported to png")
- stderr.write("STYLETRANSFER = successfully exported to png")
- Logger.critical("STYLETRANSFER = {}".format("successfully exported to png"))
- gen_img = process_img(path, self.cur_filter_id, ngrok_addr.text if ngrok_addr.text.strip() != '' else None)
- gen_img = cv2.imdecode(np.fromstring(bytes.fromhex(gen_img), np.uint8), cv2.IMREAD_COLOR)
- # res_path = os.path.join(os.path.join('res', self.filters[self.cur_filter_id]), path)
- # fl.write("\n+ successfully got styliezed image")
- stderr.write("STYLETRANSFER = successfully got styliezed image")
- Logger.critical("STYLETRANSFER = {}".format("successfully got styliezed image"))
- res_path = os.path.join(self.path_to_save, str(self.filters[self.cur_filter_id]) + '_' + im_path)
- # fl.write("\ngen img path: {}".format(res_path))
- stderr.write("STYLETRANSFER = gen img path: {}".format(os.path.abspath(res_path)))
- Logger.critical("STYLETRANSFER = {}".format("gen img path: {}".format(res_path)))
- cv2.imwrite(res_path, gen_img)
- # fl.write("\n+ successfully saved")
- stderr.write("STYLETRANSFER = successfully saved")
- Logger.critical("STYLETRANSFER = {}".format("successfully saved"))
- self.result_img.source = res_path
- # fl.write("\n+ successfully changed source")
- stderr.write("STYLETRANSFER = successfully changed source")
- Logger.critical("STYLETRANSFER = {}".format("successfully changed source"))
- self.result_img.reload()
- # fl.write("\n+ successfully reloaded")
- stderr.write("STYLETRANSFER = successfully reloaded")
- Logger.critical("STYLETRANSFER = {}".format("successfully reloaded"))
- # fl.close()
- # fl.write("\n......creating btn layout")
- stderr.write("STYLETRANSFER = ...creating btn layout")
- Logger.critical("STYLETRANSFER = {}".format("...creating btn layout"))
- btn_layout = BoxLayout(orientation='horizontal')
- but_change = Button(text='Change filter')
- but_change.bind(on_press=lambda x: change_filter(x))
- btn_layout.add_widget(but_change)
- # fl.write("\n...... + successfully add btn change to btn layout")
- stderr.write("STYLETRANSFER = ...successfully add btn change to btn layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add btn change to btn layout"))
- but_transfer = Button(text='Apply filter')
- but_transfer.bind(on_press=lambda x: apply_filter(x))
- btn_layout.add_widget(but_transfer)
- # fl.write("\n...... + successfully add btn apply to btn layout")
- stderr.write("STYLETRANSFER = ...successfully add btn apply to btn layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add btn apply to btn layout"))
- main_layout.add_widget(btn_layout)
- # fl.write("\n... + successfully add btn layout to main layout")
- stderr.write("STYLETRANSFER = ...successfully add btn layout to main layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add btn layout to main layout"))
- self.add_widget(main_layout)
- # fl.write("\n+ successfully add widget main layout")
- stderr.write("STYLETRANSFER = successfully add widget main layout")
- Logger.critical("STYLETRANSFER = {}".format("successfully add widget main layout"))
- # fl.close()
- class MyApp(App):
- def build(self):
- self.initilize_global_vars()
- sm = ScreenManager()
- sm.add_widget(MainScreen())
- return sm
- def initilize_global_vars(self):
- root_folder = os.path.dirname(self.user_data_dir)
- Logger.critical("STYLETRANSFER = {}".format("ROOT: {}".format(root_folder)))
- stderr.write("STYLETRANSFER = {}".format("ROOT: {}".format(root_folder)))
- cache_folder = os.path.join(root_folder, 'style_transfer')
- Logger.critical("STYLETRANSFER = {}".format("CACHE: {}".format(cache_folder)))
- stderr.write("STYLETRANSFER = {}".format("CACHE: {}".format(cache_folder)))
- if not os.path.exists(cache_folder):
- Logger.critical("STYLETRANSFER = {}".format("create cache folder..."))
- stderr.write("STYLETRANSFER = {}".format("create cache folder..."))
- os.makedirs(cache_folder)
- stderr.write("STYLETRANSFER = {}".format("successfully created"))
- res_path = os.path.join(cache_folder, 'res')
- if not os.path.exists(res_path):
- Logger.critical("STYLETRANSFER = {}".format("create res folder..."))
- stderr.write("STYLETRANSFER = {}".format("create res folder..."))
- os.makedirs(res_path)
- stderr.write("STYLETRANSFER = {}".format("successfully created"))
- @property
- def storage(self):
- Logger.critical("STYLETRANSFER = {}".format("storage: {}".format(os.path.join(os.path.dirname(self.user_data_dir), 'style_transfer'))))
- return os.path.join(os.path.dirname(self.user_data_dir), 'style_transfer')
- MyApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement