이재빈

Text Detect Module

1 +import cv2
2 +import numpy as np
3 +import time
4 +fname = "./croppedimg/"
5 +def bboxes(inp):
6 + #Frame을 인자로 전달받음
7 + img = inp
8 + start = time.time()
9 + index = 0
10 + # img2gray = cv2.imread(fname,0)
11 + # img = cv2.namedWindow(img,cv2.WINDOW_NORMAL)
12 + # img = cv2.resizeWindow(img,600,600)
13 + img_final = inp
14 + # img_final = cv2.namedWindow(fname,cv2.WINDOW_NORMAL)
15 + # img_final = cv2.resizeWindow(fname,600,600)
16 + img2gray = cv2.cvtColor(inp, cv2.COLOR_BGR2GRAY) #GRAY Image 8bit per pixel
17 + ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY) #threshold : distinguish background, object
18 + image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask) #bitwise
19 + ret, new_img = cv2.threshold(img_final, 180, 255, cv2.THRESH_BINARY) # Nfor black text , cv.THRESH_BINARY_IV
20 + newimg = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY) #Gray Image converting
21 +
22 + # remove noise from image
23 + kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5,1))
24 + # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more
25 + dilated = cv2.dilate(newimg, kernel, iterations=3) # dilate
26 + _, contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # get contours
27 + #cv2.CHAIN_APPROX_NONE: 모든 컨투어 포인트를 반환
28 + for contour in contours:
29 + # get rectangle bounding contour
30 + [x, y, w, h] = cv2.boundingRect(contour)
31 +
32 + # remove small false positives that aren't text
33 + # text인식하기. width, height
34 + if w < 80 and h < 80:
35 + continue
36 + if h / w > 9.0 or w / h > 9.0:
37 + continue
38 +
39 + # draw rectangle around contour on original image
40 + cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)
41 + # 이미지 주변 시작좌표를 기준으로 사각형틀그리기
42 + cropped = img_final[y :y + h , x : x + w]
43 + s = fname +"Text"+ str(index) + '.jpg'
44 + cv2.imwrite(s, cropped)
45 + if(index % 10 == 0 ):
46 + cv2.imwrite(s, cropped)
47 + print("[Processing time ]:", time.time() - start)
48 + if(index>20):
49 + index=0
50 + index = index + 1
51 +
52 + # write original image with added contours to disk
53 + imgres = cv2.resize(img, (320, 240))
54 + cv2.imshow('captcha_result', imgres)
55 +
56 +
57 +cap = cv2.VideoCapture(0) #동영상 파일 읽어옴
58 +while (cap.isOpened()):
59 + ret, inp = cap.read() #프레임을 읽어옴, 읽어온 프레임을 인자로 bboxes 전달
60 + if(ret): #success boolean
61 + bboxes(inp)
62 + if cv2.waitKey(1) & 0xFF == ord('q'):
63 + print("Terminate Process..")
64 + break
65 +cap.release() #파일 닫아줌
...\ No newline at end of file ...\ No newline at end of file