Skip to content

kookmin-sw/capstone-2022-10

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

capstone-2022-10

capstone-2022-45 created by GitHub Classroom.

ML

Crawling

ํŒŒ์ด์–ดํญ์Šค,ํฌ๋กฌ๋“ฑ์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋Š” Selenium์„ ์ด์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ํ•™์Šต๋ฐ์ดํ„ฐ

YOLOv4

์‚ฌ์šฉํ•œ YOLO V4๋ชจ๋ธ์€ You Only Look Once์˜ ์•ฝ์ž๋กœ์จ, ๊ฐ์ฒด ํƒ์ง€(Object detection)๋ถ„์•ผ์—์„œ ๋งŽ์ด ์•Œ๋ ค์ ธ ์žˆ์œผ๋ฉฐ ์ด๋ฏธ์ง€๋ฅผ ํ•œ ๋ฒˆ ๋ณด๋Š” ๊ฒƒ์œผ๋กœ ๋ฌผ์ฒด์˜ ์ข…๋ฅ˜์™€ ์œ„์น˜๋ฅผ ์ถ”์ธกํ•˜๋ฉฐ ์ด๋ฏธ์ง€์˜ ์ „์ฒด ๋งฅ๋ฝ์„ ์ดํ•ดํ•˜๋ฏ€๋กœ ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•˜๋‹ค..
YOLO v4๋Š” ์ด์ „v3๋ฒ„์ „์„ ๋”์šฑ ๊ฐœ๋Ÿ‰ํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ํ–ฅ์ƒ์‹œ์ผฐ๋‹ค.

OCR(๊ด‘ํ•™๋ฌธ์ž์ธ์‹)

๊ด‘ํ•™ ๋ฌธ์ž ์ธ์‹(Optical character recognition; OCR)์€ ์‚ฌ๋žŒ์ด ์“ฐ๊ฑฐ๋‚˜ ๊ธฐ๊ณ„๋กœ ์ธ์‡„ํ•œ ๋ฌธ์ž์˜ ์˜์ƒ์„ ์ด๋ฏธ์ง€ ์Šค์บ๋„ˆ๋กœ ํš๋“ํ•˜์—ฌ ๊ธฐ๊ณ„๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ
  • ํ…Œ์„œ๋ž™ํŠธ(Tesseract):
    • ๋‹ค์–‘ํ•œ ์šด์˜ ์ฒด์ œ๋ฅผ ์œ„ํ•œ ๊ด‘ํ•™ ๋ฌธ์ž ์ธ์‹ ์—”์ง„ ์ด ์†Œํ”„ํŠธ์›จ์–ด๋Š” Apache License, ๋ฒ„์ „ 2.0 ์— ๋”ฐ๋ผ ๋ฐฐํฌ๋˜๋Š” ๋ฌด๋ฃŒ ์†Œํ”„ํŠธ์›จ์–ด์ด๋ฉฐ 2006๋…„๋ถ€ํ„ฐ Google์—์„œ ๊ฐœ๋ฐœ์„ ํ›„์›

    • 2006๋…„ ํ…Œ์„œ๋ž™ํŠธ๋Š” ๋‹น์‹œ ๊ฐ€์žฅ ์ •ํ™•ํ•œ ์˜คํ”ˆ ์†Œ์Šค OCR ์—”์ง„ ์ค‘ ํ•˜๋‚˜๋กœ ๊ฐ„์ฃผ๋˜์—ˆ๋‹ค.

ํ•œ๊ธ€์ธ์‹์ด ์ƒ๋‹นํžˆ ์ข‹์ง€์•Š์€ํŽธ์ด๋ผ ํ•™์Šต ๋˜๋Š” ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์ด ํ•„์š”

ํ•™์Šตํ™˜๊ฒฝ

Google Colab

(์ฝ”๋žฉ์—์„œ ์ง„ํ–‰ํ•˜๋Š” ์ด์œ )

  • YOLO๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” Darknet์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”๋ฐ ์„ค์น˜ ์กฐ๊ฑด์ด ๊นŒ๋‹ค๋กญ๊ธฐ ๋•Œ๋ฌธ์— ๊ณตํ†ต์ ์ธ ํ™˜๊ฒฝ์„ ์œ„ํ•˜์—ฌ
  • Linux ํ™˜๊ฒฝ
  • GPU์—ฐ์‚ฐ ๊ฐ€๋Šฅ
#์ฃผ์˜์  : Colab ๋ฌด๋ฃŒ๋ฒ„์ „์€ ์ตœ๋Œ€ ๋Ÿฐํƒ€์ž„ ์‹œ๊ฐ„์€ 12์‹œ๊ฐ„์ด๋ฏ€๋กœ ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ๋ฅผ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฐฑ์—… ํ•„์š”

ํŒŒ์ผ๊ตฌ์กฐ

Crawling

...  
|โ”€โ”€ Crawling.py     
|โ”€โ”€ README.md
|โ”€โ”€ chromedriver  
|โ”€โ”€ requirements.txt  
...   

YOLO

...  
|โ”€โ”€ cfg/  
|   โ””โ”€โ”€custom-test-yolo.cfg   
|โ”€โ”€ data/  
|   |โ”€โ”€ labels/  
|   |โ”€โ”€ dog.jpg  
|   โ””โ”€โ”€ fruit10.jpg  
|โ”€โ”€ images/  
|   โ””โ”€โ”€ test.jpg  
|โ”€โ”€ weights/    
|   โ””โ”€โ”€ chart_custom-train-yolo.png  
|โ”€โ”€ YOLOv3.py  
|โ”€โ”€ classes.txt  
|โ”€โ”€ requirements.txt  
...   

OCR

...  
|โ”€โ”€ test_img/  
|   โ””โ”€โ”€test.jpeg
|โ”€โ”€ OCR.py
|โ”€โ”€ requirements.txt  
...  

ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

YOLO
  • YOLOํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
pip install opencv-python
pip install numpy as np 
https://drive.google.com/file/d/1-F-C5ImQSp12bAmm8VaugJwn6TGeyIgy/view?usp=sharing  
	<ํ•ด๋‹น ๋‹ค์šด๋กœ๋“œ ํŒŒ์ผ์„ YOLO/weights/ ๊ฒฝ๋กœ์— ๋„ฃ์–ด์ฃผ์„ธ์š”>
OCR
  • OCR์„ ์œ„ํ•œ ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
sudo apt install tesseract-ocr 
sudo apt-get install tesseract-ocr-kor
pip install opencv-python
pip install pytesseract

How to start

YOLO >
  • Yolo๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ
if not os.path.exists('Capstone.weights'):
        url = 'https://drive.google.com/uc?id=1-F-C5ImQSp12bAmm8VaugJwn6TGeyIgy&export=download'
        gdown.download(url, 'Capstone.weights', quiet = False)
    
##### ํ•™์ŠตํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ๋งŒ์•ฝ ์ด๋ฏธ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ๋ฌด์‹œํ•œ๋‹ค.
min_confidence = 0.1
width = 800
height = 0
show_ratio = 1.0

Weights = 'Capstone.weights'
## ํ•™์ŠตํŒŒ์ผ
file_name = "images/test.jpg"
## ํ…Œ์ŠคํŠธ ์ด๋ฏธ์ง€ 
test_cfg = "cfg/custom-test-yolo.cfg"
## YOLO configํŒŒ์ผ
net = cv2.dnn.readNetFromDarknet(test_cfg,Weights)

classes = ["๋ฌธ์–ด","์ƒˆ์†ก์ด๋ฒ„์„ฏ","๋ธ”๋ฃจ๋ฒ ๋ฆฌ","๋ฐฉ์šธํ† ๋งˆํ† ","๋ฌด", "๋ฐฐ", "์ฝฉ๋‚˜๋ฌผ"
           ,"๊ฝƒ๊ฒŒ","์–‘๋ฐฐ์ถ”", "์–‘ํŒŒ", "์ƒˆ์šฐ", "์‹œ๊ธˆ์น˜", "๊นป์žŽ", "์• ํ˜ธ๋ฐ•", "๋ฐฅ", "์˜ฅ์ˆ˜์ˆ˜"
           ,"๋งˆ๋Š˜", "๋ฐ”์ง€๋ฝ", "๊ฐ์ž", "์ˆ˜๋ฐ•", "๋ธŒ๋กœ์ฝœ๋ฆฌ", "์˜ค์ด", "๋ฉœ๋ก ", "ํŒŒ", "์˜ค์ง•์–ด"
           ,"๋‹น๊ทผ", "๋ณต์ˆญ์•„", "์ƒ์ถ”","๊ณ„๋ž€", "ํŒŒํ”„๋ฆฌ์นด", "์‚ฌ๊ณผ", "๊ณ ์ถ”", "๋ผ์ง€๊ณ ๊ธฐ", "์ฐธ์™ธ"
           ,"๋ฉธ์น˜", "๊ณ ๋“ฑ์–ด", "์กฐ๊ธฐ", "๋ฐฐ์ถ”", "๊ฐ", "๋”ธ๊ธฐ", "๊ฐ€์ง€", "์†Œ๊ณ ๊ธฐ", "๊ณ ๊ตฌ๋งˆ"
           ,"๋ฒ„ํ„ฐ", "๊ทค", "๋‹ญ๊ณ ๊ธฐ", "๋‘๋ถ€" ,"์–‘์†ก์ด๋ฒ„์„ฏ", "ํ‚ค์œ„", "๊ฐˆ์น˜"]

class_count = 50

classes.txtํŒŒ์ผ์— ์žˆ๋Š” classes ๊ฐ€์ ธ์˜ด

color_lists = np.random.uniform(0, 255, size= (len(classes), 3))
layer_names = net.getLayerNames()
output_layers = ['yolo_139', 'yolo_150', 'yolo_161']

img = cv2.imread(file_name)

h,w = img.shape[:2]
height = int(h * width / w)
blob = cv2.dnn.blobFromImage(img, 0.00392, (608,608), swapRB=True, crop=False
							 )

net.setInput(blob)
outs = net.forward(output_layers)

confidences = []
names = []
boxes = []
colors = []


for out in outs:
	for detection in out:
		scores = detection[5:]
		class_id = np.argmax(scores)
		confidence = scores[class_id]
		if confidence > min_confidence:
			#print(detection)
			# Object detected
			center_x = int(detection[0] * width)
			center_y = int(detection[1] * height)
			w = int(detection[2] * width)
			h = int(detection[3] * height)

			# Rectangle coordinates
			x = int(center_x - w /2)
			y = int(center_y - h / 2)

			boxes.append([x, y, w, h])
			confidences.append(float(confidence))
			names.append(classes[class_id])
			colors.append(color_lists[class_id])

indexes = cv2.dnn.NMSBoxes(boxes, confidences, min_confidence, 0.4)
## ์ค‘๋ณต๋œ ๋ฐ•์Šค ์ œ๊ฑฐ
print(set(names))
OCR >

์˜์ˆ˜์ฆ ์ธ์‹์„ ๊ด‘ํ•™๋ฌธ์ž์ธ์‹ OCR ์ด์šฉ

  • ๊ตฌ๊ธ€์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ด‘ํ•™๋ฌธ์ž์ต์‹ tesseract-ocr์„ ์ด์šฉ
  • kor.traineddata : ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ํŒŒ์ผ
# ํ•ด๋‹นํŒŒ์ผ์„ OCR ๋ฐ์ดํ„ฐ ํŒฉ์— ์˜ฎ๊ฒจ๋„ฃ์Œ

OpenCV๋ฅผ ์ด์šฉํ•˜์—ฌ ์™œ๊ณก ์ด๋ฏธ์ง€๋ฅผ ์›๊ทผ๋ณ€ํ™˜ ์ดํ›„ pytesseract๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜์ˆ˜์ฆ์—์„œ ํ•ด๋‹น ํ…์ŠคํŠธ ๊ฒ€์ถœ

from cv2 import INTER_AREA, INTER_LINEAR
import pytesseract
import numpy as np
import cv2

classes = ["๊ฐ€์ง€","๊ฐ์ž", "๊นป์žŽ", "๋ฒ„ํ„ฐ", "๋‹น๊ทผ",
            "๋Œ€ํŒŒ","๋งˆ๋Š˜", "๋ฌด","๋ฐฐ์ถ”","๋ธŒ๋กœ์ฝœ๋ฆฌ",
            "์ƒ์ถ”","์ƒˆ์†ก์ด๋ฒ„์„ฏ","์‹œ๊ธˆ์น˜","์• ํ˜ธ๋ฐ•",
            "์–‘๋ฐฐ์ถ”", "์–‘์†ก์ด๋ฒ„์„ฏ","์–‘ํŒŒ","์˜ค์ด",
            "๊ณ ์ถ”","๊ณ ๊ตฌ๋งˆ", "์ฝฉ๋‚˜๋ฌผ", "๊ทค","๊ฐ",
            "๋”ธ๊ธฐ", "๋ฉœ๋ก ", "์ฐธ์™ธ", "๋ฐฐ", "๋ณต์ˆญ์•„",
            "๋ธ”๋ฃจ๋ฒ ๋ฆฌ", "์‚ฌ๊ณผ", "์ˆ˜๋ฐ•", "ํŒŒํ”„๋ฆฌ์นด",
            "ํ‚ค์œ„","๋ฐฉ์šธํ† ๋งˆํ† ", "์†Œ๊ณ ๊ธฐ","๋ผ์ง€๊ณ ๊ธฐ",
            "๋‹ญ๊ณ ๊ธฐ", "๋‹ฌ๊ฑ€", "์กฐ๊ธฐ", "๊ฐˆ์น˜","๊ณ ๋“ฑ์–ด",
            "๋ฌธ์–ด", "๊ฝƒ๊ฒŒ", "์ƒˆ์šฐ", "์˜ค์ง•์–ด","๋ฐ”์ง€๋ฝ",
            "๋ฉธ์น˜", "๋‘๋ถ€", "์˜ฅ์ˆ˜์ˆ˜","๋ฐฅ"]

def order_points(pts): ## 4๊ฐœ์˜ ๊ผญ์ง€์ ์„ ์ฐพ๋Š” ํ•จ์ˆ˜
    rect = np.zeros((4, 2), dtype="float32")

    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    return rect
    
def four_point_transform(image, pts): ##4๊ฐœ์˜ ๊ผญ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ํˆฌ์˜๋ณ€ํ™˜
    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype="float32")

    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    return warped

####### ์ด๋ฏธ์ง€ ์ฝ๊ธฐ

img = cv2.imread('test_img/test2.jpg')
ratio = 600.0/img.shape[0]
dim = (int(img.shape[1] * ratio), 600)
img = cv2.resize(img, dim, interpolation= cv2.INTER_AREA)
og_img = img.copy()

GRAY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
GRAY = cv2.GaussianBlur(GRAY, (3,3), 0)
edged = cv2.Canny(GRAY, 70,200)


cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key =cv2.contourArea, reverse= True)[:5]
## ๋ฐ˜ํ™˜๋ฐ›์€ cnt์ค‘ ๋ฉด์ ์ธ ํฐ ์ˆœ์„œ๋Œ€๋กœ 5๋ฒˆ๊นŒ์ง€ ๋ฐ˜ํ™˜ 
check = False
for c in cnts:
    ## ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰
    peri = cv2.arcLength(c, True)
    ## ์ปจํˆฌ์–ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    ## ๊ธธ์ด์˜ ์˜ค์ฐจ 2ํผ์„ผํŠธ๋กœ ๋„ํ˜•์„ ๊ทผ์‚ฌํ™”
    if len(approx) == 4 and cv2.contourArea(c)>=20000:
        ## ๊ทผ์‚ฌํ™”ํ•œ ๋„ํ˜•์˜ ๊ผญ์ง€์ ์ด 4๊ฐœ๋ผ๋ฉด ๊ทธ๊ฒƒ์ด ๋ฌธ์„œ์˜ ์™ธ๊ณฝ
        screenCnt = approx
        check = True
        break
if check == False:
      img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # cv2.imshow("IMG", img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
else :
    cv2.drawContours(img, [screenCnt], -1, (0,255,0), 2)
    warped = four_point_transform(og_img, screenCnt.reshape(4, 2))
    copy = warped.copy()
    img = cv2.cvtColor(copy, cv2.COLOR_BGR2GRAY)
    
#### TEST ####
# cv2.imshow("IMG", img)
# cv2.imshow("warped", copy)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
GRAY  = img.copy()
h,w = GRAY.shape
GRAY = cv2.resize(GRAY, (2*w, 2*h), interpolation= INTER_LINEAR)
GRAY = cv2.fastNlMeansDenoising(GRAY,h=10, searchWindowSize=21,templateWindowSize=7)




min_confidence = 0.6
result = results = pytesseract.image_to_string(GRAY,lang="kor")
string = results

list = []
for i in string :
    if i.isalpha() :
        list.append(i)
    elif i == "\n" :
        list.append("\n")
    string = "".join(list)
    result = string
    result = result.replace("\n", " ")
    result = result.split(" ")
    recipe = []
for i in result :
    if i != '' :
        recipe.append(i)
        #print(recipe)
out = []
for i in recipe:
    for j in classes:
        if j in i:
            print("์ธ์‹๋œ ์žฌ๋ฃŒ๋Š” : ", j)
            out.append(j)
print(set(out))

About

capstone-2022-45 created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •