#!/usr/bin/env python # coding: utf-8 #

Table of Contents

#
# Выполнено! # In[1]: import numpy as np import pandas as pd import matplotlib.pyplot as plt import gradio as gr # In[2]: header_type = np.dtype([('chunkId', 'a4'), ('chunkSize', 'i'), ('format', 'S4'), ('subchunk1Id', 'S4'), ('subchunk1Size', np.int32), ('audioFormat', np.int16), ('numChannels', np.int16), ('sampleRate', np.int32), ('byteRate', np.int32), ('blockAlign', np.int16), ('bitsPerSample', np.int16), ('subchunk2Id', 'S', 4), ('subchunk2Size', np.int32), ]) # In[3]: bmp_header_124 = np.dtype([('type', 'S2'), ('size', np.int32, ), ('reserve', np.int16), ('reserve2', np.int16), ('offset', np.int32), ('bV5Size', 'I'), ('bV5Width', 'i'), ('bV5Height', 'i'), ('bV5Planes', 'H'), ('bV5BitCount', 'H'), ('bV5Compression', 'I'), ('bV5SizeImage', 'I'), ('bV5XPelsPerMeter', 'i'), ('bV5YPelsPerMeter', 'i'), ('bV5ClrUsed', 'I'), ('bV5ClrImportant', 'I'), ('bV5RedMask', 'I'), ('bV5GreenMask', 'I'), ('bV5BlueMask', 'I'), ('bV5AlphaMask', 'I'), ('bV5CSType', 'I'), ('bV5Endpoints', '9i'), ('bV5GammaRed', 'I'), ('bV5GammaGreen', 'I'), ('bV5GammaBlue', 'I'), ('bV5Intent', 'I'), ('bV5ProfileData', 'I'), ('bV5ProfileSize', 'I'), ('bV5Reserved', 'I')]) bmp_header_40 = np.dtype([('type', 'S2'), ('size', np.int32, ), ('reserve', np.int16), ('reserve2', np.int16), ('offset', np.int32), ('bV5Size', 'I'), ('bV5Width', 'i'), ('bV5Height', 'i'), ('bV5Planes', 'H'), ('bV5BitCount', 'H'), ('bV5Compression', 'I'), ('bV5SizeImage', 'I'), ('bV5XPelsPerMeter', 'i'), ('bV5YPelsPerMeter', 'i'), ('bV5ClrUsed', 'I'), ('bV5ClrImportant', 'I')]) bmp_dtypes = {124: bmp_header_124, 40: bmp_header_40} # In[4]: filename = r"C:\Users\Ilia\Downloads\DJI.bmp" def load_bmp(filename): if isinstance(filename, str): return_data = True else: filename = filename.name return_data = False with open(filename, 'rb') as file: header_type = np.int16(file.read(15)[14]) file.seek(0) header = np.fromfile(file, dtype = bmp_dtypes[header_type], count = 1) byte_count_per_pxl = header['bV5BitCount'][0] // 8 bmp_colors = np.dtype([('b', np.int32), ('g', np.int32), ('r', np.int32)]) colors = np.fromfile(file ,dtype = f'{byte_count_per_pxl}B', count = header['bV5SizeImage'][0]) fig, ax = plt.subplots(1, 1) colors[:, [0, 2]] = colors[:, [2, 0]] ax.imshow(np.flip(colors[:, :3].reshape((header['bV5Height'][0], header['bV5Width'][0], 3)), axis = 0)) if return_data: return header, colors else: return gr.update(value = fig, visible = True) def load_wav(filename): if isinstance(filename, str): return_data = True else: filename = filename.name return_data = False with open(filename, 'rb') as file: header = np.fromfile(file, dtype = header_type, count = 1) data = np.fromfile(file, dtype = np.int16, count = header['subchunk2Size'][0] // 2)\ .reshape(-1, header['numChannels'][0]).T.mean(axis = 0) fig, ax = plt.subplots() ax.plot(data); if return_data: return header, data else: return [gr.update(value = fig, visible = True), gr.update(value = (header[0][7], data), visible = True)]; # In[5]: with gr.Blocks() as app: with gr.Row(): with gr.Column(): wav_file = gr.File(label = 'Загрузите WAV файл', file_count = 'single', file_types = ['.wav']) wav_btn = gr.Button(value = 'Обработать WAV') bmp_file = gr.File(label = 'Загрузите BMP файл', file_count = 'single', file_types = ['.bmp']) bmp_btn = gr.Button(value = 'Обработать BMP') with gr.Column(): wav_plot = gr.Plot(visible = False) audio = gr.Audio(visible = False) bmp_plot = gr.Plot(visible = False) wav_btn.click(load_wav, inputs = wav_file, outputs = [wav_plot, audio]) bmp_btn.click(load_bmp, inputs = bmp_file, outputs = [bmp_plot]) # In[6]: app.launch()