반응형

컴퓨터/파이썬 (Python) 8

[Python] 폴더 내 파일 명 추출해서 txt 파일로 저장하기

import os f = open("new.txt", 'w') filenames = os.listdir() for filename in filenames: data = filename+"\n" f.write(data) print("저장완료") f.close() 특정한 폴더 내의 파일들 파일 명을 추출하여 저장하는 코드임. =>> txt 파일로 저장됨 특정 폴더 내로 하고싶으면 os.chdir('폴더위치') 코드를 추가해야 할 것임 그것도 귀찮으면 그냥 원하는 폴더에서 파이썬 실행시키면 됨.

[Python] wav 파일 자르기

import wave # 원하는 시간 단위 (초) start = 1 # 시작 시간, seconds end = 300 # 끝나는 시간, seconds # file to extract the snippet from with wave.open('test.wav', "rb") as infile: # get file data nchannels = infile.getnchannels() sampwidth = infile.getsampwidth() framerate = infile.getframerate() # set position in wave to start of segment infile.setpos(int(start * framerate)) # extract data data = infile.readfram..

[Python] wav 파일 Speech-to-Text 파이썬 코드

import speech_recognition as sr r = sr.Recognizer() text = sr.AudioFile('Source.wav') with text as source: audio = r.record(source) print("Source:"+r.recognize_google(audio)) 5분 이상 되는 음원은 오류가 나는 것 같음. 음원을 자르는 파이썬 코드는 아래에 있음. https://hb2207.tistory.com/54 [Python] wav 파일 자르기 import wave # 원하는 시간 단위 (초) start = 1 # 시작 시간, seconds end = 300 # 끝나는 시간, seconds # file to extract the snippet from with ..

[Python] image to csv

from PIL import Image import numpy as np im = Image.open('img2.png') pixels = list(im.getdata()) pixel_list =[] print(len(pixels)) myArray = np.array(pixels) myArray = myArray.astype(int) np.savetxt("pixel_data.csv", myArray, delimiter=", ", newline=" ",fmt='%i') PIL을 이용하여 이미지를 pixel으로 변환하는 코드. 딥러닝 모델에 직접 제작한 이미지 파일을 사용하고 싶은데 모델에서 인풋을 csv로 변경해야하기 때문에 이런 코드가 필요했다. 저장 할 때 integer로 저장되고 1칸에 값들이 1차원..

[Python, 머신러닝] Confusion Matrix 그리기

학습 데이터 정확도를 확인을 위해 Accuracy를 많이 사용한다. 그러나 Accuracy는 balance한 데이터 셋이 아니라면 결과를 확인하기에 부족하다. 따라서 balance 하지 않은 데이터 셋의 정확도를 확인하고 싶다면, Confusion matrix를 확인해보아야 한다. 코드는 아래와 같으며 원하는 multiclass에 원하는 값으로 수정하여 사용하면 된다. +) 라벨도 바꾸어 주어야 한다. 예시 코드 from sklearn.metrics import confusion_matrix import seaborn as sns from mlxtend.plotting import plot_confusion_matrix import matplotlib.pyplot as plt import numpy as..