Cov txheej txheem:

Neural Network Powered Planetarium Siv Python, Electron, thiab Keras: 8 Kauj Ruam
Neural Network Powered Planetarium Siv Python, Electron, thiab Keras: 8 Kauj Ruam

Video: Neural Network Powered Planetarium Siv Python, Electron, thiab Keras: 8 Kauj Ruam

Video: Neural Network Powered Planetarium Siv Python, Electron, thiab Keras: 8 Kauj Ruam
Video: Neural Network In 5 Minutes | What Is A Neural Network? | How Neural Networks Work | Simplilearn 2024, Hlis ntuj nqeg
Anonim
Neural Network Powered Planetarium Siv Python, Electron, thiab Keras
Neural Network Powered Planetarium Siv Python, Electron, thiab Keras

Hauv cov lus qhia no, kuv yuav qhia koj li cas kuv tau sau lub tshuab hluav taws xob 3D planetarium tsis siv neeg, siv Python thiab Electron

Cov vis dis aus saum toj no qhia txog ib qho ntawm cov xwm txheej ntiaj chaw uas tsim los.

** Nco tseg: Txoj haujlwm no tsis muaj txoj hauv kev zoo, thiab qee qhov chaw tsis zoo heev. Kev ntxub ntxaug ntawm neural tsuas yog ~ 89% raug, yog li qee cov duab txawv txawv yuav ua rau nws mus rau hauv lub ntiaj chaw **

Yam tshwj xeeb

Lub ntiaj chaw tshawb nrhiav NASA API rau cov duab ntsig txog chaw, thiab siv kev sib txuas sib txuas neural los txiav txim seb daim duab puas haum rau kev ua. Qhov kev zov me nyuam siv OpenCV tshem tawm keeb kwm yav dhau los ntawm daim duab, thiab thaum kawg cov duab tau sib txuas ua ke rau hauv ib qho duab loj sib npaug. Cov duab no tau txais kev cawmdim, thiab Electron Node.js daim ntawv thov qhib cov duab, thiab siv pob PhotoSphere.js los saib cov duab hauv lub ntiaj teb zoo li 3D hom.

Kev vam khom

Nab hab sej:

  • Keras
  • Hauv ncoo
  • cv2 ua
  • Quaj
  • Kev thov
  • urllib
  • Random
  • sij hawm
  • ua io

Electron:

PhotoSphere

Kauj Ruam 1: Teeb Koj Qhov Chaw

Txhim kho Electron thiab Python

Ua ntej, xyuas kom koj muaj node.js thiab npm teeb tsa (yog tias tsis yog, koj tuaj yeem rub tawm ntawm no)

Tom ntej no, koj yuav tsum teeb tsa Electron. Qhib qhov hais kom ua sai, thiab sau cov lus txib hauv qab no:

npm nruab electron -g

Tom ntej no, koj xav tau nab hab sej, uas tuaj yeem rub tawm ntawm no

Teeb tsa ib puag ncig Virtual

Qhib cov lus txib kom sai, tom qab ntawd nkag mus rau cov lus txib hauv qab no los teeb tsa koj li chaw ib puag ncig:

pip nruab virtualenv

virtualenv chaw

cd chaw

scripts / qhib

Txhim kho Python Dependencies

Khiav cov lus txib no hauv qhov hais kom ua txhawm rau txhim kho koj li nab nab vam khom:

pip nruab keras

pip nruab hauv ncoo

pip nruab numpy

pip nruab thov

pip nruab opencv-pythonYog tias koj xav cob qhia lub network koj tus kheej, nco ntsoov teeb tsa GPU nrawm rau Keras

Kauj Ruam 2: Nug NASA NASA Search API

Txheej txheem cej luam

NASA muaj ntau qhov APIs muaj txiaj ntsig zoo uas koj tuaj yeem siv nrog koj cov phiaj xwm. Txog qhov haujlwm no, peb yuav siv tshawb fawb API, uas tso cai rau peb tshawb nrhiav NASA cov duab database rau cov duab ntsig txog chaw.

Txoj Cai

Ua ntej, peb yuav tsum tau txhais cov nab npawb ua haujlwm kom lees txais qhov kev sib cav uas yuav ua raws li cov lus tshawb fawb:

def get_image_search (kab lus):

dhau

Tom ntej no, peb yuav hloov lo lus tshawb fawb mus rau hom ntawv URL, tom qab ntawd siv lub tsev qiv ntawv thov los nug API:

def get_image_search (kab lus):

params = {"q": urllib.parse.quote (arg), "media_type": "duab"} cov txiaj ntsig = thov.get ("https://images-api.nasa.gov/search", params = params)

Thaum kawg, peb yuav txiav txim siab qhov sau+JSON txoj hlua uas API rov qab los rau peb, thiab rho tawm cov npe txuas mus rau cov duab ntsig txog lub sijhawm tshawb nrhiav:

def get_image_search (kab lus):

params = {"q": urllib.parse.quote (arg), "media_type": "duab"} cov txiaj ntsig = thov.get ("https://images-api.nasa.gov/search", params = params) data = [tshwm sim ['href'] rau qhov tshwm sim hauv results.json () ["sau"] ["cov khoom"]

Peb mus! Tam sim no peb muaj cov lej code uas tuaj yeem nug NASA duab tshawb API, thiab xa cov npe txuas mus rau cov duab ntsig txog peb lub sijhawm tshawb nrhiav.

Kauj Ruam 3: Kev Sib Koom Neural Network

Txheej txheem cej luam

Txoj haujlwm ntawm lub paj hlwb yog txhawm rau txheeb xyuas seb daim duab yog ntawm qee yam hauv qhov chaw, lossis txawm tias nws tsis yog. Txhawm rau ua qhov no, peb yuav siv kev sib txuas ntawm neural network, lossis CNN, los ua ntu ntu ntawm kev ua haujlwm ntawm daim duab, thiab txiav txim siab tias qhov chaw-y nws yog li cas. Kuv yuav tsis piav qhov no tag nrho, vim tias muaj ntau txoj kev xav tom qab nws, tab sis yog tias koj xav kawm txog neural tes hauj lwm, Kuv qhia "Machine Learning Mastery"

Txoj Cai

Ua ntej, peb yuav tsum tau ntshuam peb cov kev vam khom:

import os

#Kho qhov teeb meem thaum caij tsheb ciav hlau oN GPU os.environ ['CUDA_VISIBLE_DEVICES'] = 'ntshuam tensorflow li tf yog tf.test.gpu_device_name (): print (' GPU pom ') else: print ("Tsis pom GPU") los ntawm keras.preprocessing.image ntshuam ImageDataGenerator los ntawm keras.preprocessing ntshuam duab los ntawm keras.models ntshuam Sequential los ntawm keras.layers import Conv2D, MaxPooling2D los ntawm keras.layers import Activation, Dropout, Flatten, Tsom los ntawm keras import backend li K los ntawm PIL ntshuam duab import numpy as np

Tom ntej no peb yuav tsum txhais peb tus qauv:

img_width, img_height = 1000, 500

train_data_dir = 'v_data/train' validation_data_dir = 'v_data/test' nb_train_samples = 203 nb_validation_samples = 203 epochs = 10 batch_size = 8 yog K.image_data_format () == 'channel_first': input_shapeight (img) = (img_width, img_height, 3) model = Sequential () model.add (Conv2D (32, (2, 2), input_shape = input_shape)) model.add (Activation ('relu')) model.add (MaxPooling2D (pool_size) = (2, 2))) model.add (Conv2D (32, (2, 2))) model.add (Ua kom ('relu')) model.add (MaxPooling2D (pool_size = (2, 2)))) qauv.add (Conv2D (64, (2, 2))) model.add (Activation ('relu')) model.add (MaxPooling2D (pool_size = (2, 2))) model.add (Flatten ()) qauv. ntxiv (Dense (64)) model.add (Activation ('relu')) model.add (Dropout (0.5)) model.add (Dense (1)) model.add (Activation ('sigmoid')) model.compile (poob = 'binary_crossentropy', optimizer = 'rmsprop', ntsuas = ['raug'])

Kuv tau kawm tus qauv rau koj, tab sis yog tias koj xav qhia tus qauv rau koj tus kheej, ntawm koj tus kheej cov ntaub ntawv, tom qab ntawd kuv tau txuas qhov kev qhia ua haujlwm. Txwv tsis pub, koj tuaj yeem rub tawm tus qauv kawm HDF5 cov ntaub ntawv. Vim yog cov ntaub ntawv txwv kev txwv, Kuv yuav tsum tau hloov nws nrog ".txt" txuas ntxiv. Txhawm rau siv nws, hloov npe cov ntaub ntawv mus rau ".h5" txuas ntxiv, thiab thauj nws nrog cov lej no:

model.load_weights ("qauv_saved.h5")

Txhawm rau siv lub network los twv seb qhov chaw-y cov duab yog li cas, peb yuav txhais lub luag haujlwm no:

def twv ua ntej (image_path):

img = image.load_img (image_path, target_size = (1000, 500)) img = np.expand_dims (img, axis = 0) result = model.predict_classes (img) return result [0] [0]

Kauj Ruam 4: Ua Tus Duab

Txheej txheem cej luam

Txog kev ua duab, Kuv tab tom siv OpenCV (cv2) lub tsev qiv ntawv. Ua ntej, peb yuav plooj qhov ntug ntawm daim duab, thiab tom qab ntawd peb yuav tshem tawm keeb kwm yav dhau los los ntawm kev tsim daim npog ntsej muag thiab hloov pauv alpha qhov tseem ceeb ntawm cov xim tsaus

Txoj Cai

Nov yog ib feem ntawm txoj haujlwm uas ua rau lub ntsej muag tsis meej:

def txheej txheem Img (img):

RADIUS = 20 # Qhib cov duab im = Image.open ("pilbuffer.png") # Muab cov duab tso rau ntawm daim duab keeb kwm yav dhau dawb = 2 * RADIUS rov qab = Image.new ('RGB', (im.size [0] + diam, im.size [1] + diam), (0, 0, 0)) back.paste (im, (RADIUS, RADIUS)) # Tsim lub ntsej muag plooj npog = Image.new ('L', (im.size [0] + diam, im.size [1] + diam), 255) blck = Image.new ('L', (im.size [0] - diam, im.size [1] - diam), 0) daim npog. muab tshuaj txhuam (blck, (diam, diam)) # Plooj duab thiab muab cov npoo plooj raws li daim npog ntsej muag plooj = rov qab.filter (ImageFilter. GaussianBlur (RADIUS / 2)) back.paste (plooj, daim npog = daim npog ntsej muag) back.save (" transition-p.webp

Tom ntej no, peb yuav teeb cov xim tsaus rau kom pob tshab, thiab khaws cov duab ib ntus:

#Tsim daim npog ntsej muag thiab lim hloov xim dub nrog alpha

image = cv2.imread ("transition.png") hMin = 0 sMin = 0 vMin = 20 hMax = 180 sMax = 255 vMax = 255 lower = np.array ([hMin, sMin, vMin]) upper = np.array ([hMax, sMax, vMax]) hsv = cv2.cvtColor (duab, cv2. COLOR_BGR2HSV) daim npog = cv2.inRange (hsv, qis, sab saud) output = cv2.bitwise_and (duab, duab, daim npog = daim npog) *_, alpha = cv2.split (output) dst = cv2.merge ((output, alpha)) output = dst with open ("buffer.png", "w+") as file: pass cv2.imwrite ("buffer.png", tso tawm)

Kauj Ruam 5: Daws Cov Duab Ua Ntej Rau Hauv Qhov Kev Ntsuas Zoo

Txheej txheem cej luam

Txoj haujlwm no siv ntau cov duab thiab xaws lawv rau hauv hom uas tuaj yeem txhais los ntawm PhotoSphere.js pob, siv PIL (hauv ncoo) lub tsev qiv ntawv

Txoj Cai

Ua ntej, peb yuav tsum tsim cov duab uas tuaj yeem ua tus tswv tsev rau lwm cov duab:

tshiab = Image.new ("RGBA", (8000, 4000), xim = (0, 0, 0))

Tom ntej no, peb yuav tsum rov ua dua los ntawm cov duab (uas txhua qhov tau hloov mus rau 1000x500) thiab muab tso rau hauv cov duab:

h = 0 os

w = 0 i = 0 rau img hauv img_arr: tshiab.paste (img, (w, h), img) w += 1000 yog w == 8000: h += 500 w = 0 i += 1

Tam sim no peb tsuas yog qhwv qhov no hauv txoj haujlwm uas siv ntau cov duab raws li nws qhov kev sib cav, thiab xa cov duab tshiab:

def stitch_beta (img_arr): cov

tshiab = Image.new ("RGBA", (8000, 4000), xim = (0, 0, 0)) h = 0 w = 0 i = 0 rau img hauv img_arr: tshiab.paste (img, (w, h), img) w += 1000 if w == 8000: h += 500 w = 0 i += 1 return new

Kauj Ruam 6: Tsab Ntawv Python Tag Nrho

Nov yog tag nrho cov nab hab sej neural network tsab ntawv, uas tau khaws tseg raws li net.py, thiab nkag mus rau hauv tsab ntawv tseem ceeb:

# importing cov tsev qiv ntawv

ntshuam os #Fix rau qhov teeb meem thaum caij tsheb ciav hlau oN GPU os.environ ['CUDA_VISIBLE_DEVICES'] = '' ntshuam tensorflow li tf yog tf.test.gpu_device_name (): print ('GPU pom') else: print ("Tsis pom GPU ") los ntawm keras.preprocessing.image ntshuam ImageDataGenerator los ntawm keras.preprocessing ntshuam duab los ntawm keras.models ntshuam Sequential los ntawm keras.layers ntshuam Conv2D, MaxPooling2D los ntawm keras.layers ntshuam qhib, tso tawm, Ua kom ncaj, ntom los ntawm keras import backend li K los ntawm PIL import Image import numpy as np img_width, img_height = 1000, 500 train_data_dir = 'v_data/train' validation_data_dir = 'v_data/test' nb_train_samples = 203 nb_validation_samples = 203 epochs = 10 batch_size = 8 if K.image_): input_shape = (3, img_width, img_height) lwm yam: input_shape = (img_width, img_height, 3) qauv = Sequential () model.add (Conv2D (32, (2, 2), input_shape = input_shape)) model.add (Ua kom ('relu')) model.add (MaxPooling2D (pool_size = (2, 2)))) model.add (Conv2D (32, (2, 2)))) qauv. ntxiv (Ua kom ('relu')) model.add (MaxPooling2D (pool_size = (2, 2)))) model.add (Conv2D (64, (2, 2)))) model.add (Ua kom ('relu')) model.add (MaxPooling2D (pool_size = (2, 2))) model.add (Flatten ()) model.add (Dense (64)) model.add (Activation ('relu')) model.add (Dropout (0.5)) model.add (Dense (1)) model.add (Activation ('sigmoid')) model.compile (poob = 'binary_crossentropy', optimizer = 'rmsprop', ntsuas = ['raug']) model.load_weights ("model_saved.h5") def predict (image_path): img = image.load_img (image_path, target_size = (1000, 500)) img = np.expand_dims (img, axis = 0) result = model.predict_classes (img) rov qab los [0] [0]

Nov yog cov ntaub ntawv sej tseem ceeb, api.py:

thov thov, sys, random, urllib.parse, cv2

los ntawm PIL ntshuam duab, ImageFilter los ntawm io import BytesIO import numpy li np ntshuam net def get_image_search (tus lej, kab lus): suav = 0 img_arr = rau arg hauv kab lus: print (arg) print (f "Tam sim no suav cov duab: {suav } ") i = 0 params = {" q ": urllib.parse.quote (arg)," media_type ":" duab "} cov txiaj ntsig = thov.get (" https://images-api.nasa.gov/search ", params = params) data = [result ['href'] for result in results.json () [" collection "] [" items "] print (len (data)) if num> len (data): num = len (cov ntaub ntawv) thaum suav = num: so print (f "\ n {count} images retreived") return img_arr def stitch_beta (img_arr): tshiab = Image.new ("RGBA", (8000, 4000), xim = (0, 0, 0)) h = 0 w = 0 i = 0 rau img hauv img_arr: #pbar.set_description (f "Ua cov duab {i +1}") new.paste (img, (w, h), img) w += 1000 yog w == 8000: h += 500 w = 0 i += 1 rov ua qhov txheej txheem def tshiabImage (img): RADIUS = 20 # Qhib ib qho duab im = Image.open ("pilbuffer.png") # Muab cov duab tso rau ntawm cov duab keeb kwm yav dhau dawb = 2 * RADIUS rov qab = Image.new ('RGB', (im.size [0] + diam, im.size [1] + diam), (0, 0, 0)) back.paste (im, (RADIUS, RADIUS)) # Tsim lub ntsej muag plooj npog = Image.new ('L', (im.size [0] + diam, im.size [1] + diam), 255) blck = Image.new ('L', (im.size [0] - diam, im.size [1] - diam), 0) mask.paste (blck, (diam, diam)) # Plooj duab thiab muab cov plooj plooj plooj raws li daim npog qhov muag plooj = back.filter (ImageFilter. GaussianBlur (RADIUS / 2)) back.paste (plooj, daim npog = daim npog) back.save ("transition.png") back.close () #Create daim npog thiab lim hloov dub nrog duab alpha = cv2.imread (" tsheb thauj mus los ion.png ") hMin = 0 sMin = 0 vMin = 20 hMax = 180 sMax = 255 vMax = 255 lower = np.array ([hMin, sMin, vMin]) upper = np.array ([hMax, sMax, vMax])) hsv = cv2.cvtColor (duab, cv2. COLOR_BGR2HSV) daim npog = cv2.inRange (hsv, qis, sab saud) tso tawm = cv2.bitwise_and (duab, duab, daim npog = daim npog) *_, alpha = cv2.split (tso tawm) dst = cv2.merge ((output, alpha)) output = dst with open ("buffer.png", "w+") as file: pass cv2.imwrite ("buffer.png", output) #Edge detection thiab blurring yog _name_ == "_main_": search_terms = ["supernova", "planet", "galaxy", "milky way", "nebula", "stars"] #Cov ntsiab lus tshawb fawb tuaj yeem hloov kho rau txhua yam koj xav kom lub ntiaj teb suav nrog img_arr = get_image_search (64, search_terms) luam tawm ("Cov duab rub tawm thiab neural lim") img = stitch_beta (img_arr) print ("Duab stitched") img.save ("stitched.png")

Kauj Ruam 7: Electron App

Txheej txheem cej luam

Peb yuav tsim lub tshuab hluav taws xob yooj yim uas tsuas yog ua haujlwm thiab thauj khoom PhotoSphere. Cov ntaub ntawv tseem ceeb.js thiab pob.json ncaj qha los ntawm Electron lub vev xaib, thiab HTML yog qhov hloov kho me ntsis ntawm HTML muab rau ntawm PhotoSphere lub vev xaib. Kuv tau suav nrog cov ntaub ntawv, tab sis hloov npe tag nrho rau.txt, raws li Cov Lus Qhia tsis tso cai rau hom ntawv no. Txhawm rau siv cov ntawv, hloov lawv nrog qhov tsim nyog txuas ntxiv.

Txoj Cai

main.js

const {app, BrowserWindow} = xav tau ('electron')

muaj nuj nqi createWindow () {const yeej = BrowserWindow tshiab ({dav: 800, qhov siab: 600, webPreferences: {nodeIntegration: tseeb}}) win.loadFile ('index.html')} app.whenReady (). ces (createWindow) app.on ('window-all-closed', () => {if (process.platform! == 'darwin') {app.quit ()}}) app.on ('qhib', () => {yog (BrowserWindow.getAllWindows (). ntev === 0) {createWindow ()}})

pob.json

{

"name": "space", "version": "0.1.0", "main": "main.js", "scripts": {"start": "electron." }}

index.html

Kauj ruam 8: Kev ua tiav

Tsim cov duab sib npaug

Txhawm rau tsim cov duab, khiav cov ntawv api.py hauv qhov hais kom ua sai sai, nrog nws qhov chaw ib puag ncig tau qhib:

api.py ua

Tom qab cov ntawv sau tiav, khiav lub tshuab hluav taws xob siv:

npm nceVoila! Koj lub hnub qub ci ntsa iab! Ua tsaug rau kev nyeem:)

Pom zoo: