def on_received_data(data): if data[0] == "S": global img_width global img_height global img_size global t_debut_algo global macroblock_size global A t_debut_algo = time() split_data = data.split(".") # split_data[0] = "SIZE_INFO" img_width = int(split_data[1]) img_height = int(split_data[2]) # format standard img_size = (img_width, img_height) log.debug(f"Taille reçue : {img_size}") macroblock_size = int(split_data[3]) A = Encoder.DCT_operator(macroblock_size) log.debug(f"Macroblock_size reçu : {macroblock_size}") print("") else: global received_data received_data += data binary_MSG_TYPE = data[16 : 18] # "11" est en fait la valeur de TAIL_MSG (= 3) en binaire if binary_MSG_TYPE == "11": decode_frame_entierement() received_data = ""
generer_fichier_log = False affiche_debug = True log = Logger.get_instance() log.set_log_level(LogLevel.DEBUG) if generer_fichier_log: log.start_file_logging("log_émetteur_vidéo.log") # Valeurs standards de macroblock_size : 8, 16 et 32 (valeur conseillée : 16) # Doit être <= 63 macroblock_size = 16 # création de l'opérateur orthogonal de la DCT A = Encoder.DCT_operator(macroblock_size) enc = Encoder() #----------------------------------------------------------------------------# """ Transformation de la vidéo considérée en liste de frames """ print("") log.debug( f"Conversion \"vidéo {video_name} --> liste de frames\" en cours ...") OS = sys.platform if OS == "win32": video_path = getcwd() + "\\assets\\" + video_name
def execute_main(total_nb_of_images): """ Exécute le programme principal "total_nb_of_images" fois afin d'extraire des statistiques sur notre algorithme de compression (du coup sur un total de "total_nb_of_images" images). """ # # # -------------------------USEFUL DATA-------------------------- # # # t_debut_analyse = time() global log log = Logger.get_instance() log.set_log_level(LogLevel.DEBUG) print("") log.debug(f"Début de l'analyse des {total_nb_of_images} images\n") global possible_macroblock_sizes possible_macroblock_sizes = [8, 16, 32] global dico_huff_ratios dico_huff_ratios = {8 : [], 16 : [], 32 : []} global dico_dict_ratios dico_dict_ratios = {8 : [], 16 : [], 32 : []} global dico_metadata_ratios dico_metadata_ratios = {8 : [], 16 : [], 32 : []} global dico_comp_rates dico_comp_rates = {8 : [], 16 : [], 32 : []} global dec enc = Encoder() dec = Decoder() bufsize = 4096 HOST = "localhost" PORT = 3456 serv = Server(HOST, PORT, bufsize, affiche_messages=False) cli = Client(HOST, PORT, bufsize, affiche_messages=False) global received_data received_data = "" serv.listen_for_packets(callback=on_received_data) cli.connect_to_server() frame_id = 0 for possible_macroblock_size in possible_macroblock_sizes: global macroblock_size macroblock_size = possible_macroblock_size # création de l'opérateur orthogonal de la DCT global A A = Encoder.DCT_operator(macroblock_size) for image_nb in range(1, total_nb_of_images + 1): frame_id += 1 global img_counter img_counter = image_nb # # # --------------------IMAGE GENERATION---------------------- # # # nom_image = f"image_{image_nb}.jpg" OS = sys.platform if OS == "win32": path_image = getcwd() + "\\assets\\image_testing\\" + nom_image elif OS == "linux" or OS == "linux2": path_image = getcwd() + "/assets/image_testing/" + nom_image else: log.error(f"Unrecognized platform : {OS}") sys.exit() image = Image.open(path_image) global img_width global img_height global img_size img_size = image.size # pour l'instant : que du 96x64 pixels img_width, img_height = img_size image_intermediaire = image.getdata() image_rgb = np.array(image_intermediaire).reshape((img_height, img_width, 3)) # # # ----------------------IMAGE ENCODING---------------------- # # # # frame RGB --> frame YUV image_yuv = enc.RGB_to_YUV(np.array(image_rgb, dtype=float)) # frame YUV --> frame RLE global rle_data rle_data = enc.decompose_frame_en_macroblocs_via_DCT(image_yuv, img_size, macroblock_size, DEFAULT_QUANTIZATION_THRESHOLD, A) # # # ----------------SENDING DATA OVER NETWORK----------------- # # # # frame RLE --> bitstream --> réseau global bit_sender bit_sender = BitstreamSender(frame_id, img_size, macroblock_size, rle_data, cli, bufsize) bit_sender.start_sending_messages() # on termine le thread d'écriture dans le buffer du bitstream bit_sender.th_WriteInBitstreamBuffer.join() cli.send_data_to_server("FIN_ENVOI") # fermeture du socket créé, du côté serveur ET du côté client # on termine également le thread d'écoute du serveur cli.connexion.shutdown(2) # 2 = socket.SHUT_RDWR cli.connexion.close() serv.th_Listen.join() serv.mySocket.close() # pour laisser le temps au message associé à la fermeture de la connexion du # client de se print correctement sleep(0.1) log.debug("Thread d'écoute du serveur supprimé.") log.debug("Serveur supprimé.") print("") log.debug(f"Fin de l'analyse des {total_nb_of_images} images") t_fin_analyse = time() duree_analyse = t_fin_analyse - t_debut_analyse log.info(f"Durée de l'analyse : {duree_analyse:.3f} s")