2 분 소요

python 폴더 내 이미지 읽기

import os
 
root_dir = 'C:/Users/Sim/Desktop/test' # 디렉토리
 
img_path_list = []
possible_img_extension = ['.jpg', '.jpeg', '.JPG', '.bmp', '.png'] # 이미지 확장자들
 
for (root, dirs, files) in os.walk(root_dir):
    if len(files) > 0:
        for file_name in files:
            if os.path.splitext(file_name)[1] in possible_img_extension:
                img_path = root + '/' + file_name
                
                # 경로에서 \를 모두 /로 바꿔줘야함
                img_path = img_path.replace('\\', '/') # \는 \\로 나타내야함         
                img_path_list.append(img_path)
                             
print(img_path_list)



python 폴더 선택

import tkinter
from tkinter import filedialog

root = tkinter.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
print("\ndir_path : ", dir_path)


폴더 선택하여 차량 내부 검출하기

import os
import tkinter
from tkinter import filedialog

root = tkinter.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')

img_path_list = []
possible_img_extension = ['.jpg', '.jpeg', '.JPG', '.bmp', '.png'] # 이미지 확장자들
 
for (root, dirs, files) in os.walk(dir_path):
    if len(files) > 0:
        for file_name in files:
            if os.path.splitext(file_name)[1] in possible_img_extension:
                img_path = root + '/' + file_name
                img_output_path = root+'/Output/'+file_name
                # 경로에서 \를 모두 /로 바꿔줘야함
                img_path = img_path.replace('\\', '/') # \는 \\로 나타내야함         
                img_path_list.append(img_path)

차량 검출 예제 사용해보기


# importing necessary libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt
import os
import tkinter
from tkinter import filedialog


def LineCrackDetect(input,output):
    # read a cracked sample image
#    img = cv2.imread('Input-Set/Cracked_07.jpg')
    print(input)
    img_array = np.fromfile(input, np.uint8)
    img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
#    img = cv2.imread(input)

    # Convert into gray scale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Image processing ( smoothing )
    # Averaging
    blur = cv2.blur(gray,(3,3))

    # Apply logarithmic transform
    img_log = (np.log(blur+1)/(np.log(1+np.max(blur))))*255

    # Specify the data type
    img_log = np.array(img_log,dtype=np.uint8)

    # Image smoothing: bilateral filter
    bilateral = cv2.bilateralFilter(img_log, 5, 75, 75)

    # Canny Edge Detection
    edges = cv2.Canny(bilateral,100,200)

    # Morphological Closing Operator
    kernel = np.ones((5,5),np.uint8)
    closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    # Create feature detecting method
    # sift = cv2.xfeatures2d.SIFT_create()
    # surf = cv2.xfeatures2d.SURF_create()
    orb = cv2.ORB_create(nfeatures=1500)

    # Make featured Image
    keypoints, descriptors = orb.detectAndCompute(closing, None)
    featuredImg = cv2.drawKeypoints(closing, keypoints, None)

    # Create an output image
    #cv2.imwrite('Output-Set/CrackDetected-7.jpg', featuredImg)
    extension = os.path.splitext(output)[1] # 이미지 확장자
    result, encoded_img = cv2.imencode(extension, featuredImg)
    if result:
        with open(output, mode='w+b') as f:
            encoded_img.tofile(f)


root = tkinter.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')

img_path_list = []
possible_img_extension = ['.jpg', '.jpeg', '.JPG', '.bmp', '.png'] # 이미지 확장자들
 
for (root, dirs, files) in os.walk(dir_path):
    img_output_path = root+'/Output/'
    os.mkdir(img_output_path)
    if len(files) > 0:
        for file_name in files:
            if os.path.splitext(file_name)[1] in possible_img_extension:
                img_path = root + '/' + file_name
                img_output_path = root+'/Output/'+file_name
                # 경로에서 \를 모두 /로 바꿔줘야함
                img_path = img_path.replace('\\', '/') # \는 \\로 나타내야함         
                img_path_list.append(img_path)

                LineCrackDetect(img_path,img_output_path)



출처

  1. python 폴더 내 이미지 읽기 + 한글 이미지 읽기 https://bskyvision.com/entry/%EC%95%84%EC%9D%B4%EC%BD%98%EA%B3%BC-%ED%8C%8C%EB%B9%84%EC%BD%98%EC%9D%98-%EC%B0%A8%EC%9D%B4

https://bskyvision.com/entry/python-cv2imread-%ED%95%9C%EA%B8%80-%ED%8C%8C%EC%9D%BC-%EA%B2%BD%EB%A1%9C-%EC%9D%B8%EC%8B%9D%EC%9D%84-%EB%AA%BB%ED%95%98%EB%8A%94-%EB%AC%B8%EC%A0%9C-%ED%95%B4%EA%B2%B0-%EB%B0%A9%EB%B2%95

  1. python 폴더 선택 https://cjsal95.tistory.com/35

  2. 차량 불량 검출 https://github.com/shomnathsomu/crack-detection-opencv

댓글남기기