Forezz

Server_nn

Feb 13th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. from flask import Flask, request
  2. from flask_restful import Api, Resource, reqparse
  3. import werkzeug
  4. from Video_nn import VideoNN
  5. import os
  6. import time
  7. import threading
  8.  
  9. app = Flask(__name__)
  10. api = Api(app)
  11.  
  12.  
  13. parser = reqparse.RequestParser()
  14. parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
  15.  
  16.  
  17. def check_info(info_path):
  18.     if not os.path.isdir(info_path):
  19.         os.mkdir(info_path)
  20.     info_files = ['all_updated.txt', 'new_updated.txt', 'order.txt']
  21.     for file in info_files:
  22.         if not os.path.exists(info_path + file):
  23.             open(file, 'w')
  24.  
  25. class HellowWOrld(Resource):
  26.     def __init__(self):
  27.         self.is_proceccing = False
  28.         self.information_path = 'information/'
  29.         self.content_path = 'content/'
  30.         check_info(self.information_path)
  31.  
  32.     def post(self, pictures_folder):
  33.         pictures_path = self.content_path + pictures_folder
  34.         upd_pictures_path = self.content_path + 'Updated_' + pictures_folder
  35.         if not os.path.exists(pictures_path):
  36.             os.mkdir(pictures_path)
  37.         if not os.path.exists(upd_pictures_path):
  38.             os.mkdir(upd_pictures_path)
  39.  
  40.         args = parser.parse_args()
  41.         picture = args['picture']
  42.         args_realsr = ''.split()
  43.  
  44.         if picture:
  45.             if not self.is_proceccing:
  46.                 future_filename = 'Updated_' + pictures_folder + '/' \
  47.                                   + self.process_picture(picture, pictures_path, upd_pictures_path, *args_realsr)
  48.  
  49.                 self.after_processing_thread = threading.Thread(target=self.after_processing,
  50.                                                                 args=(self, future_filename,),
  51.                                                                 name='after_proccessing')
  52.                 self.after_processing_thread.start()
  53.             else:
  54.                 return {'status': 'Wait some times. Computer is busy now by other picture'}
  55.  
  56.             return {"status": "Picture was uploaded", 'futureName': future_filename}
  57.         else:
  58.             return {"status": "No picture in request"}
  59.  
  60.     def process_picture(self, picture, pictures_path, upd_pictures_path, *args_realsr):
  61.         self.add_information(pictures_path + picture.filename)
  62.         filenameWOE = picture.filename.split('.')[0]
  63.         picture.save('{}/{}'.format(pictures_path, picture.filename))
  64.  
  65.         self.processing_thread = threading.Thread(target=VideoNN.use_realsr,
  66.                                                   args=(VideoNN, pictures_path + '/' + picture.filename,
  67.                                                            upd_pictures_path + '/' + filenameWOE + '.png',
  68.                                                            *args_realsr,),
  69.                                   kwargs= {
  70.                                       'pathToRealsr': '/home/vladt/PycharmProjects/VideoNN/realsr-ncnn-vulkan/realsr-ncnn-vulkan'},
  71.                                              name='processing')
  72.  
  73.         self.processing_thread.start()
  74.         return filenameWOE + '.png'
  75.  
  76.     def after_processing(self, picture_filename):
  77.         while not self.processing_thread.is_alive():
  78.             self.add_information(picture_filename, 'all_updated.txt')
  79.             self.add_information(picture_filename, 'new_updated.txt')
  80.             self.delete_information('order.txt')
  81.  
  82.     def add_information(self, picture_filename, infofile, first=True):
  83.         check_info(self.information_path)
  84.         file_path = self.information_path + infofile
  85.         with open(file_path, 'r+') as file:
  86.             text = file.readlines()
  87.             if first:
  88.                 text = [picture_filename + '\n'] + text
  89.             else:
  90.                 if (text[-1] == '\n') or (not text):
  91.                     del text[-1]
  92.                 text += [picture_filename]
  93.             file.seek(0)
  94.             for row in text:
  95.                 file.write(row)
  96.  
  97.     def delete_information(self, infofile):
  98.         check_info(self.information_path)
  99.         file_path = self.information_path + infofile
  100.         with open(file_path, 'r+') as file:
  101.             text = file.readlines()
  102.             text = text[1:]
  103.             file.seek(0)
  104.             for row in text:
  105.                 file.write(row)
  106.     # def delete(self, pictures_folder):
  107.  
  108. api.add_resource(HellowWOrld, '/content/<string:pictures_folder>')
  109.  
  110. if __name__ == "__main__":
  111.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment