https://stackoverflow.com/questions/58349726/opencv-how-to-remove-text-from-background
분류 전체보기
png중 배경이 투명한 이미지를 jpg처럼 흰 배경을 추가한 후, 변환 PIL Image 활용 image = Image.open("test.png").convert("RGBA") new_image = Image.new("RGBA", image.size, "WHITE") new_image.paste(image, mask=image) new_image.convert("RGB").save("test.jpg") cv2 활용 import cv2 #load image with alpha channel. use IMREAD_UNCHANGED to ensure loading of alpha channel image = cv2.imread('your image', cv2.IMREAD_UNCHANGED) #make mas..
![](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FwEnLl%2FbtrxY4iEjjP%2FfJkaiBXvigZXc3un3GoZ41%2Fimg.jpg)
다음과 같이 문장이 있는 image가 존재할 때, 문장 안에 있는 space(띄어 쓰기)마다 단어를 잘라 저장하고 싶을 때. 차례대로 순서를 나열하자면, 1) 글자의 굵기를 늘린다. 2) 겹쳐진 글자를 하나의 단어 뭉치로 본다. 3) cv2의 Contour찾는 함수로 단어들을 찾아낸다. 4) boundingRect값으로 기존 이미지를 단어 단위로 잘라내기 +) cv2상 Contour 찾는거는 검은 배경에서 흰색 물건을 찾는거라 생각하면 간단 => 따라서, 찾고자 하는 글자를 흰색, 배경을 검게 해야 함 => 그렇기 때문에 배경이 없는 png는 곤란함 cv2만을 사용하지만 PIL은 내가 따로 사용하려고, plt는 jupyter notebook에서 확인하기 위해서 import했다. import cv2 imp..
https://076923.github.io/posts/Python-opencv-1/ Python OpenCV 강좌 : 제 1강 - OpenCV 설치 OpenCV 076923.github.io
PIL로 Image open했는데 자동으로 돌아가는 경우 나는 분명 정방향 이미지를 주었는데 로딩했을 때 지 멋대로 돌아가는 경우가 존재한다. 이는 보통 핸드폰으로 찍은 사진에서 EXIF 안에 방향 태그가 존재할 경우, 발생하는 문제로 보인다. https://github.com/python-pillow/Pillow/issues/4703#issuecomment-645219973 PIL.Image.open is rotating jpeg images · Issue #4703 · python-pillow/Pillow Hello, I am trying to load images with Image.open. However, the images are autorotated if they were jpg images..
보통 List를 사용할 때 내부 중복 제거를 위해서 단순히 set으로 중복을 제거한 후, 다시 list로 변환하는 경우가 많은것 같다. 나 역시도 간단했기 때문에 제일 많이 사용한 방식이다. sample_list = [1, 2, 3, 3, 4, 4, 5] new_list = list(set(sample_list)) >>> new_list >>> [3, 1, 4, 2, 5] 다만, Set을 사용하는 방식은 내부에 쌓아두었던 list의 순서가 무너진다는 단점이 있다. Python의 set 자체가 중복을 허용하지않으며, 동시에 Set 내부의 값들은 순서가 존재하지 않는 값으로 정의하기 때문이다. 이렇기 때문에 기존의 list 내부에 쌓아둔 값 순서를 유지하되, 중복을 제거할 경우 별도의 for문을 통해 신규 ..
보통 GPU에서 embedding size가 맞지 않는 것과 같은 데이터 또는 모델링 문제가 대다수라 한다. 다만, 나같은 경우엔 이미 Huggingface에서 제공하는 Transformer모델을 사용하기 때문에 Input이나 모델링은 따로 건들지 않아도 됐다. 좀 더 디테일한 에러 문구가 없나 찾아보던 도중 CUDA(GPU)로 돌리던 코드를 CPU로만 돌리면 디테일한 문구가 나온다 하여 CUDA_VISIBLE_DEVICES="" 로 돌려보거나 코드 상단에 아래와 같은 코드를 넣어 CPU로 돌리니 실제 hugging face 내부 함수에서 등록되어 있던 에러 문구가 등장해서 해결할 수 있었다. import os os.environ['CUDA_LAUNCH_BLOCKING'] = "1" #아니면 아래 문구로..
![](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcN2a2G%2FbtrwhylPOXy%2FyPTSnA2MjiIlIQHC4P3DM0%2Fimg.png)
단순히 이미지 두개를 겹칠 때는 아래처럼 paste함수로 해주면 된다. from PIL import Image image1 = Image.open("./image1.png") image2 = Image.open("./image2.png") image1 = image1.resize((500, 500)) image2 = image2.resize((300, 300)) #이미지 1번의 정 중앙에 놓고 싶어서 btsize = tuple(np.subtract(image1.size,image2.size)) new_image = Image.new('RGB',(500, 500), (255,2555,255)) new_image.paste(image1,(0,0)) new_image.paste(image2,(btsize[0]..
![](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fc2VgEe%2FbtrwiyR4gvs%2F742srkkxqKqLktpvVjPQ6k%2Fimg.png)
cv2.putText가 간단한데 한글이 깨짐 PIL로 한글 깨짐 방지하는 코드 상단 txt파일에 있는 글자를 이미지에 같이 표현하게끔 코드 작성 겉에 있는 bbox는 cv2의 polylines 활용 import cv2 import numpy as np import os from PIL import ImageFont, ImageDraw, Image # Define Document Type input_doc = 'directory_name' file_list = os.listdir('./gts/%s'%input_doc) file_name = [file.split('.')[0][3:] for file in file_list] image_list = ['./images/%s/'%input_doc+file+'.j..
from glob import glob import os dir_lst = os.listdir("./outputs") for dirs in dir_lst: input_files = "./outputs/%s/*"%dirs for dir_n in glob(input_files): path = dir_n+"/json/" if len(os.listdir(path))==0: print("No_Json:%s"%path) else: continue 멀티프로세싱 프로그램 구동 중 특정 파일이 미생성되는 경우가 발생. 해당 파일의 존재유무를 파악하기 위한 간단한 python 코드
![](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FQB3BD%2FbtrvYv4maSb%2F5YpR6NLzchPWOnn27vdoB0%2Fimg.png)
G마켓 best품목 중 각 클래스에 대해서 100개의 상품이 나열되어 있음 이 중 상품 명, 가격을 크롤링 한 후, 파일로 저장 >페이지에서 특정 값을 갖고 오고 싶을 땐 인터넷 창에서 F12눌러서 해당 파트 찾은 후, 우클릭 한다음 copy>copy selector를 통해 복사하면 자동으로 해당 파트를 받아오는 코드가 카피된다. ex) #skip-navigation-container > div.section__main.section__main-best > div > div > div > div > button.button__next 이 값을 Beautifulsoup 객체의 select_one 함수의 input으로 넣음 된다. import requests from bs4 import BeautifulSo..
print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB', 'Cached:', round(torch.cuda.memory_cached(0)/1024**3,1), 'GB') #or MB = 1024.0 * 1024.0 memory=torch.cuda.max_memory_allocated() / MB
파일 삭제 # 특정 파일 삭제할 때 import os os.remove("./test.txt") 특정 파일(or 확장자) 삭제 glob 설치 : pip install glob2 import os from glob import glob # .py확장자로 끝나는 파일을 다 삭제하고 싶을 때 [os.remove(f) for f in glob.glob("./test/*.py") 특정 폴더 내 전체 파일 삭제 dirPath = "./test" if os.path.exists(dirPath): for file in os.scandir(dirPath): print("Remove File: ",file) os.remove(file) 특정 폴더 포함, 폴더 내 전체 파일도 함께 삭제 shutil 설치 : pip inst..
# PIP INSTALL pip install ipywidgets jupyter nbextension enable --py widgetsnbextension # ANACONDA INSTALL conda install -c conda-forge ipywidgets +) 참고 블로그 : https://woongheelee.com/entry/%EC%97%90%EB%9F%AC%ED%95%B4%EA%B2%B0%EB%B2%95-ImportError-IProgress-not-found 에러해결법 ImportError: IProgress not found 연구실에 딥러닝 연구를 위한 멀티 GPU 장착 서버가 일곱대있다(더 있남??). 평소 안쓰던 놈에 텐서플로우 2 설치하고 텐서플로우 데이터셋(TensorFlow Da..