Spaces:
Sleeping
Sleeping
Commit
•
01d61b8
1
Parent(s):
09717fe
Upload bmp_reader.py
Browse files- bmp_reader.py +112 -0
bmp_reader.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# <h1>Table of Contents<span class="tocSkip"></span></h1>
|
5 |
+
# <div class="toc"><ul class="toc-item"></ul></div>
|
6 |
+
|
7 |
+
# Выполнено!
|
8 |
+
|
9 |
+
# In[1]:
|
10 |
+
|
11 |
+
|
12 |
+
import numpy as np
|
13 |
+
import pandas as pd
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
import gradio as gr
|
16 |
+
|
17 |
+
|
18 |
+
# In[2]:
|
19 |
+
|
20 |
+
|
21 |
+
header_type = np.dtype([('chunkId', 'a4'), ('chunkSize', 'i'), ('format', 'S4'), ('subchunk1Id', 'S4'), ('subchunk1Size', np.int32),
|
22 |
+
('audioFormat', np.int16), ('numChannels', np.int16), ('sampleRate', np.int32), ('byteRate', np.int32),
|
23 |
+
('blockAlign', np.int16), ('bitsPerSample', np.int16), ('subchunk2Id', 'S', 4), ('subchunk2Size', np.int32),
|
24 |
+
])
|
25 |
+
|
26 |
+
|
27 |
+
# In[3]:
|
28 |
+
|
29 |
+
|
30 |
+
bmp_header_124 = np.dtype([('type', 'S2'), ('size', np.int32, ), ('reserve', np.int16), ('reserve2', np.int16), ('offset', np.int32),
|
31 |
+
('bV5Size', 'I'), ('bV5Width', 'i'), ('bV5Height', 'i'), ('bV5Planes', 'H'), ('bV5BitCount', 'H'),
|
32 |
+
('bV5Compression', 'I'), ('bV5SizeImage', 'I'), ('bV5XPelsPerMeter', 'i'), ('bV5YPelsPerMeter', 'i'),
|
33 |
+
('bV5ClrUsed', 'I'), ('bV5ClrImportant', 'I'), ('bV5RedMask', 'I'), ('bV5GreenMask', 'I'),
|
34 |
+
('bV5BlueMask', 'I'), ('bV5AlphaMask', 'I'), ('bV5CSType', 'I'),
|
35 |
+
('bV5Endpoints', '9i'), ('bV5GammaRed', 'I'), ('bV5GammaGreen', 'I'), ('bV5GammaBlue', 'I'),
|
36 |
+
('bV5Intent', 'I'), ('bV5ProfileData', 'I'), ('bV5ProfileSize', 'I'), ('bV5Reserved', 'I')])
|
37 |
+
bmp_header_40 = np.dtype([('type', 'S2'), ('size', np.int32, ), ('reserve', np.int16), ('reserve2', np.int16), ('offset', np.int32),
|
38 |
+
('bV5Size', 'I'), ('bV5Width', 'i'), ('bV5Height', 'i'), ('bV5Planes', 'H'), ('bV5BitCount', 'H'),
|
39 |
+
('bV5Compression', 'I'), ('bV5SizeImage', 'I'), ('bV5XPelsPerMeter', 'i'), ('bV5YPelsPerMeter', 'i'),
|
40 |
+
('bV5ClrUsed', 'I'), ('bV5ClrImportant', 'I')])
|
41 |
+
bmp_dtypes = {124: bmp_header_124, 40: bmp_header_40}
|
42 |
+
|
43 |
+
|
44 |
+
# In[4]:
|
45 |
+
|
46 |
+
|
47 |
+
filename = r"C:\Users\Ilia\Downloads\DJI.bmp"
|
48 |
+
def load_bmp(filename):
|
49 |
+
if isinstance(filename, str):
|
50 |
+
return_data = True
|
51 |
+
else:
|
52 |
+
filename = filename.name
|
53 |
+
return_data = False
|
54 |
+
with open(filename, 'rb') as file:
|
55 |
+
header_type = np.int16(file.read(15)[14])
|
56 |
+
file.seek(0)
|
57 |
+
header = np.fromfile(file, dtype = bmp_dtypes[header_type], count = 1)
|
58 |
+
byte_count_per_pxl = header['bV5BitCount'][0] // 8
|
59 |
+
bmp_colors = np.dtype([('b', np.int32), ('g', np.int32), ('r', np.int32)])
|
60 |
+
colors = np.fromfile(file ,dtype = f'{byte_count_per_pxl}B', count = header['bV5SizeImage'][0])
|
61 |
+
fig, ax = plt.subplots(1, 1)
|
62 |
+
colors[:, [0, 2]] = colors[:, [2, 0]]
|
63 |
+
ax.imshow(np.flip(colors[:, :3].reshape((header['bV5Height'][0], header['bV5Width'][0], 3)), axis = 0))
|
64 |
+
if return_data:
|
65 |
+
return header, colors
|
66 |
+
else:
|
67 |
+
return gr.update(value = fig, visible = True)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
def load_wav(filename):
|
72 |
+
if isinstance(filename, str):
|
73 |
+
return_data = True
|
74 |
+
else:
|
75 |
+
filename = filename.name
|
76 |
+
return_data = False
|
77 |
+
with open(filename, 'rb') as file:
|
78 |
+
header = np.fromfile(file, dtype = header_type, count = 1)
|
79 |
+
data = np.fromfile(file, dtype = np.int16, count = header['subchunk2Size'][0] // 2)\
|
80 |
+
.reshape(-1, header['numChannels'][0]).T.mean(axis = 0)
|
81 |
+
fig, ax = plt.subplots()
|
82 |
+
ax.plot(data);
|
83 |
+
if return_data:
|
84 |
+
return header, data
|
85 |
+
else:
|
86 |
+
return [gr.update(value = fig, visible = True), gr.update(value = (header[0][7], data), visible = True)];
|
87 |
+
|
88 |
+
|
89 |
+
# In[5]:
|
90 |
+
|
91 |
+
|
92 |
+
with gr.Blocks() as app:
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
wav_file = gr.File(label = 'Загрузите WAV файл', file_count = 'single', file_types = ['.wav'])
|
96 |
+
wav_btn = gr.Button(value = 'Обработать WAV')
|
97 |
+
bmp_file = gr.File(label = 'Загрузите BMP файл', file_count = 'single', file_types = ['.bmp'])
|
98 |
+
bmp_btn = gr.Button(value = 'Обработать BMP')
|
99 |
+
|
100 |
+
with gr.Column():
|
101 |
+
wav_plot = gr.Plot(visible = False)
|
102 |
+
audio = gr.Audio(visible = False)
|
103 |
+
bmp_plot = gr.Plot(visible = False)
|
104 |
+
wav_btn.click(load_wav, inputs = wav_file, outputs = [wav_plot, audio])
|
105 |
+
bmp_btn.click(load_bmp, inputs = bmp_file, outputs = [bmp_plot])
|
106 |
+
|
107 |
+
|
108 |
+
# In[6]:
|
109 |
+
|
110 |
+
|
111 |
+
app.launch(share = True)
|
112 |
+
|