Code
|
Ta’rifi
|
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
|
Biz avvalo kerakli kutubxonalarni yuklaymiz va import qilib olamiz
|
input_folder = '../input/'
filename = input_folder+'digits.png'
image = Image.open(filename)
|
Rasmlarni saqlash uchun papka manzilini tanlaymiz va bizga kerakli rasmlarni ochishni boshlaymiz
|
def show(img, figsize=(8, 4), title=None):
plt.figure(figsize=figsize)
plt.imshow(img)
if title:
plt.title(title)
plt.show()
|
Show() funksiyasini yaratamiz va u bizga kerakli rasmlarni ochib beradi
|
print(f'Image details')
print(f'Format {image.format}')
print(f'Size {image.size}')
print(f'Mode {image.mode}')
|
Rasmni tahlil qilamiz
|
from PIL import ImageEnhance
bw_image = image.convert(mode='L')
show(bw_image, figsize=(12, 12))
bw_image = ImageEnhance.Contrast(bw_image).enhance(1.5)
show(bw_image, figsize=(12, 12))
|
ImageEnhance funksiyasini import qilib olamiz va rasmni ko’rib chiqamiz
|
f = plt.figure(figsize=(18,2))
ax = f.subplots(1, 6)
for i, n in enumerate(np.random.randint(0, len(samples), size=6)):
m = (np.random.randint(0, len(samples[n])))
ax[i].imshow(samples[n][m])
ax[i].set_title(f'Digit: [{n}]')
plt.show()
|
Tanlangan rasmlarni alohida chiqarib ularnitahlil qilamiz
|
preview = Image.new('L', (len(samples[0])*new_size, len(samples)*new_size))
x = 0
y = 0
for row in resized_samples:
for sample in row:
preview.paste(sample, (x, y))
x += new_size
y+=new_size
x = 0
show(preview, figsize=(18,18), title='Processed images')
preview.save('preview.png')
|
Kiritilgan sonlarni chiqarish va saqlashni boshlaymiz
|
from PIL import ImageOps
import matplotlib.patches as patches
inv_sample = ImageOps.invert(sample)
bbox = inv_sample.getbbox()
fig = plt.figure(figsize=(2, 2))
ax = fig.add_axes([0,0,1,1])
ax.imshow(inv_sample)
rect = patches.Rectangle(
(bbox[0], bbox[3]), bbox[2]-bbox[0], -bbox[3]+bbox[1]-1,
fill=False, alpha=1, edgecolor='w')
ax.add_patch(rect)
plt.show()
|
Tanlangan raqamni alohida belgilaymiz va aniqlab chiqaramiz
|
binary_samples = np.array([[sample.getdata() for sample in row] for row in resized_samples])
binary_samples = binary_samples.reshape(len(resized_samples)*len(resized_samples[0]), 28, 28)
xfile = 'digits_x_test.npy'
yfile = 'digits_y_test.npy'
np.save(xfile, binary_samples)
np.save(yfile, classes)
x_test = np.load(xfile)
y_test = np.load(yfile)
x_test.shape, y_test.shape
for i in np.random.randint(x_test.shape[0], size=6):
show(x_test[i], title=f'Digit [{y_test[i]}]', figsize=(1,1))
|
Hosil qilingan datasetdan malumotlarni olamiz va tekshiramiz
|
|