Advertisement
Guest User

main

a guest
Dec 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.83 KB | None | 0 0
  1. '''
  2. Shuffled Camera Feed Puzzle
  3. ===========================
  4.  
  5. This demonstrates using Scatter widgets with a live camera.
  6. You should see a shuffled grid of rectangles that make up the
  7. camera feed. You can drag the squares around to see the
  8. unscrambled camera feed or double click to scramble the grid
  9. again.
  10. '''
  11. import os
  12. import time
  13. from sys import stderr
  14. import cv2
  15. import numpy as np
  16. from kivy import Logger
  17. from kivy.app import App
  18. from kivy.app import dirname
  19. from kivy.uix.boxlayout import BoxLayout
  20. from kivy.uix.button import Button
  21. from kivy.uix.camera import Camera
  22. from kivy.uix.image import Image
  23. from kivy.uix.textinput import TextInput
  24. from kivy.uix.screenmanager import Screen, ScreenManager
  25. from kivy.utils import platform
  26. from kivy.uix.widget import Widget
  27. from kivy.uix.slider import Slider
  28. from kivy.uix.scatter import Scatter
  29. from kivy.animation import Animation
  30. from kivy.graphics import Color, Rectangle
  31. from kivy.properties import NumericProperty
  32. from random import randint, random
  33. from functools import partial
  34.  
  35. from client import *
  36.  
  37. DIR = ''
  38.  
  39.  
  40. class MainScreen(Screen):
  41.     def __init__(self, **kwargs):
  42.         super(MainScreen, self).__init__(**kwargs)
  43.         self.path_to_src = App.get_running_app().storage #os.path.join('/sdcard', 'DCIM') if platform == 'android' else ''
  44.         self.path_to_save = os.path.join(self.path_to_src, 'res')
  45.         self.path_to_styles = os.path.join(self.path_to_src, 'styles')
  46.         # self.log_file_path = os.path.join(self.path_to_src, 'log.txt')
  47.  
  48.         # fl = open(self.log_file_path, 'a+')
  49.         # fl.write('\n\n\nNEW SESS--------------------------------')
  50.  
  51.         self.camera = Camera(resolution=(640, 480), play=True)
  52.         # fl.write("\n+ successfuly set camera")
  53.         stderr.write("STYLETRANSFER = successfuly set camera")
  54.         Logger.critical("STYLETRANSFER = {}".format("successfuly set camera"))
  55.  
  56.         self.filters = ['mosaic', 'wave']
  57.         # fl.write("\n+ successfuly set filters names")
  58.         stderr.write("STYLETRANSFER = successfuly set filters names")
  59.         Logger.critical("STYLETRANSFER = {}".format("successfuly set filters names"))
  60.  
  61.         self.cur_filter_id = 0
  62.         # fl.write("\n+ successfuly set cur filter id")
  63.         stderr.write("STYLETRANSFER = successfuly set cur filter id")
  64.         Logger.critical("STYLETRANSFER = {}".format("successfuly set cur filter id"))
  65.  
  66.         self.style_img = Image(source=os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id])), size=(640, 480))
  67.         # fl.write("\n+ successfuly set style image source")
  68.         stderr.write("STYLETRANSFER = successfuly set style image source")
  69.         Logger.critical("STYLETRANSFER = {}".format("successfuly set style image source {}".format(os.path.abspath(self.style_img.source))))
  70.  
  71.         self.result_img = Image(source="2.jpeg", size=(640, 480))
  72.         # fl.write("\n+ successfuly set results image source")
  73.         stderr.write("STYLETRANSFER = successfuly set results image source")
  74.         Logger.critical("STYLETRANSFER = {}".format(
  75.             "successfuly set result image source {}".format(os.path.abspath(self.result_img.source))))
  76.  
  77.         # fl.write("\n...creating main layout")
  78.         stderr.write("STYLETRANSFER = ...creating main layout")
  79.         Logger.critical("STYLETRANSFER = {}".format("...creating main layout"))
  80.         main_layout = BoxLayout(orientation='vertical')
  81.  
  82.         ngrok_addr = TextInput(font_size='20sp', height=30, size_hint=(None, None))
  83.         main_layout.add_widget(ngrok_addr)
  84.  
  85.         # fl.write("\n......creating img layout")
  86.         stderr.write("STYLETRANSFER = ...creating img layout")
  87.         Logger.critical("STYLETRANSFER = {}".format("...creating img layout"))
  88.         img_layout = BoxLayout(orientation='horizontal')
  89.         img_layout.add_widget(self.camera)
  90.         # fl.write("\n...... + successfully add camera to img layout")
  91.         stderr.write("STYLETRANSFER = ...successfully add camera to img layout")
  92.         Logger.critical("STYLETRANSFER = {}".format("successfully add camera to img layout"))
  93.         img_layout.add_widget(self.style_img)
  94.         # fl.write("\n...... + successfully add style img to img layout")
  95.         stderr.write("STYLETRANSFER = ...successfully add style img to img layout")
  96.         Logger.critical("STYLETRANSFER = {}".format("successfully add style img to img layout"))
  97.         img_layout.add_widget(self.result_img)
  98.         # fl.write("\n...... + successfully add res img to img layout")
  99.         stderr.write("STYLETRANSFER = ...successfully add res img to img layout")
  100.         Logger.critical("STYLETRANSFER = {}".format("successfully add res img to img layout"))
  101.  
  102.         main_layout.add_widget(img_layout)
  103.         # fl.write("\n... + successfuly add img layout to  main layout")
  104.         stderr.write("STYLETRANSFER = ...successfuly add img layout to  main layout")
  105.         Logger.critical("STYLETRANSFER = {}".format("successfully add img layout to main layout"))
  106.  
  107.  
  108.         def change_filter(instance):
  109.             # fl = open(self.log_file_path, 'a+')
  110.             # fl.write("\nCHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters)))
  111.             stderr.write("STYLETRANSFER = CHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters)))
  112.             Logger.critical("STYLETRANSFER = {}".format("\nCHANGE FILTER: {} -> {}".format(self.cur_filter_id, (self.cur_filter_id + 1) % len(self.filters))))
  113.             self.cur_filter_id = (self.cur_filter_id + 1) % len(self.filters)
  114.             print('cur id: {}'.format(self.cur_filter_id))
  115.             print("{}.jpg".format(self.filters[self.cur_filter_id]))
  116.             # fl.write("\nnew style img path: {}".format(os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id]))))
  117.             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])))))
  118.             Logger.critical("STYLETRANSFER = {}".format("new style img path: {}".format(os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id])))))
  119.             self.style_img.source = os.path.join(self.path_to_styles, "{}.jpg".format(self.filters[self.cur_filter_id]))
  120.             # fl.write("\n+ successfully changed source")
  121.             stderr.write("STYLETRANSFER = successfully changed source")
  122.             Logger.critical("STYLETRANSFER = {}".format("successfully changed source"))
  123.             self.style_img.reload()
  124.             # fl.write("\n+ successfully reloaded")
  125.             stderr.write("STYLETRANSFER = successfully reloaded")
  126.             Logger.critical("STYLETRANSFER = {}".format("successfully reloaded"))
  127.             # fl.close()
  128.  
  129.         def apply_filter(instance):
  130.             # fl = open(self.log_file_path, 'a+')
  131.             # fl.write("\nAPPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id]))
  132.             stderr.write("STYLETRANSFER = APPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id]))
  133.             Logger.critical("STYLETRANSFER = {}".format("APPLY FILTER {} ({})".format(self.cur_filter_id, self.filters[self.cur_filter_id])))
  134.             timestr = time.strftime("%Y%m%d_%H%M%S")
  135.             im_path = "IMG_{}.png".format(timestr)
  136.             path = os.path.join(self.path_to_save, im_path)
  137.  
  138.             # fl.write("\nimg path: {}".format(path))
  139.             stderr.write("STYLETRANSFER = img path: {}".format(os.path.abspath(path)))
  140.             Logger.critical("STYLETRANSFER = {}".format("img path: {}".format(path)))
  141.             self.camera.export_to_png(path)
  142.             # fl.write("\n+ successfully exported to png")
  143.             stderr.write("STYLETRANSFER = successfully exported to png")
  144.             Logger.critical("STYLETRANSFER = {}".format("successfully exported to png"))
  145.  
  146.             gen_img = process_img(path, self.cur_filter_id, ngrok_addr.text if ngrok_addr.text.strip() != '' else None)
  147.             gen_img = cv2.imdecode(np.fromstring(bytes.fromhex(gen_img), np.uint8), cv2.IMREAD_COLOR)
  148.             # res_path = os.path.join(os.path.join('res', self.filters[self.cur_filter_id]), path)
  149.             # fl.write("\n+ successfully got styliezed image")
  150.             stderr.write("STYLETRANSFER = successfully got styliezed image")
  151.             Logger.critical("STYLETRANSFER = {}".format("successfully got styliezed image"))
  152.             res_path = os.path.join(self.path_to_save, str(self.filters[self.cur_filter_id]) + '_' + im_path)
  153.             # fl.write("\ngen img path: {}".format(res_path))
  154.             stderr.write("STYLETRANSFER = gen img path: {}".format(os.path.abspath(res_path)))
  155.             Logger.critical("STYLETRANSFER = {}".format("gen img path: {}".format(res_path)))
  156.             cv2.imwrite(res_path, gen_img)
  157.             # fl.write("\n+ successfully saved")
  158.             stderr.write("STYLETRANSFER = successfully saved")
  159.             Logger.critical("STYLETRANSFER = {}".format("successfully saved"))
  160.             self.result_img.source = res_path
  161.             # fl.write("\n+ successfully changed source")
  162.             stderr.write("STYLETRANSFER = successfully changed source")
  163.             Logger.critical("STYLETRANSFER = {}".format("successfully changed source"))
  164.             self.result_img.reload()
  165.             # fl.write("\n+ successfully reloaded")
  166.             stderr.write("STYLETRANSFER = successfully reloaded")
  167.             Logger.critical("STYLETRANSFER = {}".format("successfully reloaded"))
  168.             # fl.close()
  169.  
  170.         # fl.write("\n......creating btn layout")
  171.         stderr.write("STYLETRANSFER = ...creating btn layout")
  172.         Logger.critical("STYLETRANSFER = {}".format("...creating btn layout"))
  173.  
  174.         btn_layout = BoxLayout(orientation='horizontal')
  175.  
  176.         but_change = Button(text='Change filter')
  177.         but_change.bind(on_press=lambda x: change_filter(x))
  178.         btn_layout.add_widget(but_change)
  179.         # fl.write("\n...... + successfully add btn change to btn layout")
  180.         stderr.write("STYLETRANSFER = ...successfully add btn change to btn layout")
  181.         Logger.critical("STYLETRANSFER = {}".format("successfully add btn change to btn layout"))
  182.  
  183.         but_transfer = Button(text='Apply filter')
  184.         but_transfer.bind(on_press=lambda x: apply_filter(x))
  185.         btn_layout.add_widget(but_transfer)
  186.         # fl.write("\n...... + successfully add btn apply to btn layout")
  187.         stderr.write("STYLETRANSFER = ...successfully add btn apply to btn layout")
  188.         Logger.critical("STYLETRANSFER = {}".format("successfully add btn apply to btn layout"))
  189.  
  190.         main_layout.add_widget(btn_layout)
  191.         # fl.write("\n... + successfully add btn layout to main layout")
  192.         stderr.write("STYLETRANSFER = ...successfully add btn layout to main layout")
  193.         Logger.critical("STYLETRANSFER = {}".format("successfully add btn layout to main layout"))
  194.  
  195.         self.add_widget(main_layout)
  196.         # fl.write("\n+ successfully add widget main layout")
  197.         stderr.write("STYLETRANSFER = successfully add widget main layout")
  198.         Logger.critical("STYLETRANSFER = {}".format("successfully add widget main layout"))
  199.         # fl.close()
  200.  
  201.  
  202. class MyApp(App):
  203.  
  204.     def build(self):
  205.         self.initilize_global_vars()
  206.         sm = ScreenManager()
  207.         sm.add_widget(MainScreen())
  208.         return sm
  209.  
  210.     def initilize_global_vars(self):
  211.         root_folder = os.path.dirname(self.user_data_dir)
  212.         Logger.critical("STYLETRANSFER = {}".format("ROOT: {}".format(root_folder)))
  213.         stderr.write("STYLETRANSFER = {}".format("ROOT: {}".format(root_folder)))
  214.         cache_folder = os.path.join(root_folder, 'style_transfer')
  215.         Logger.critical("STYLETRANSFER = {}".format("CACHE: {}".format(cache_folder)))
  216.         stderr.write("STYLETRANSFER = {}".format("CACHE: {}".format(cache_folder)))
  217.  
  218.         if not os.path.exists(cache_folder):
  219.             Logger.critical("STYLETRANSFER = {}".format("create cache folder..."))
  220.             stderr.write("STYLETRANSFER = {}".format("create cache folder..."))
  221.             os.makedirs(cache_folder)
  222.             stderr.write("STYLETRANSFER = {}".format("successfully created"))
  223.  
  224.         res_path = os.path.join(cache_folder, 'res')
  225.         if not os.path.exists(res_path):
  226.             Logger.critical("STYLETRANSFER = {}".format("create res folder..."))
  227.             stderr.write("STYLETRANSFER = {}".format("create res folder..."))
  228.             os.makedirs(res_path)
  229.             stderr.write("STYLETRANSFER = {}".format("successfully created"))
  230.  
  231.     @property
  232.     def storage(self):
  233.         Logger.critical("STYLETRANSFER = {}".format("storage: {}".format(os.path.join(os.path.dirname(self.user_data_dir), 'style_transfer'))))
  234.         return os.path.join(os.path.dirname(self.user_data_dir), 'style_transfer')
  235.  
  236.  
  237. MyApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement