반응형

Selenium과 Chrome driver를 이용해 웹싸이트에서 로그인이 필요한 정보를 가져 올 수 있다.

아래 코드는 인터넷 서점(Yes24)에 접속해서 로그인 하고 주문 내역을 가져 온다.

Chrome이 동작하는걸 보고 싶다면 options 관련 부분만 삭제하면 된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from selenium import webdriver
 
login_url = "https://www.yes24.com/Templates/FTLogin.aspx"
id = "아이디"
password = "패스워드"
 
options = webdriver.ChromeOptions()
options.add_argument("headless"# headless 모드
options.add_argument("window-size=1920x1080"# headless 모드가 아니면 의미가 없는거 같다.
options.add_argument("--log-level=3"# 이 코드가 없으면 headless 모드시 log가 많이 표시 된다.
 
#driver = webdriver.Chrome("c:\download\chromedriver.exe", options = options)
driver = webdriver.Chrome(options = options) # webdriver와 소스가 같은 폴더에 있을 때.
driver.implicitly_wait(3)
driver.get(login_url)
print("Title: %s\nLogin URL: %s" %(driver.title, driver.current_url))
 
id_elem = driver.find_element_by_id("SMemberID")
id_elem.clear()
id_elem.send_keys(id)
 
driver.find_element_by_id("SMemberPassword").clear()
driver.find_element_by_id("SMemberPassword").send_keys(password)
# clear()와 send_keys()는 None을 리턴 한다.
 
login_btn = driver.find_element_by_xpath('//*[@id="btnLogin"]')
login_btn.click() # None 리턴
# driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
 
list_url = "https://ssl.yes24.com//MyPageOrderList/MyPageOrderList"
driver.get(list_url)
 
print("-" * 30"Order list""-" * 30)
lists = driver.find_elements_by_css_selector("#MyOrderListTbl span")
for i, item in enumerate(lists):
    print("%d: %s" %(i + 1, item.text))
 
driver.save_screenshot("screenshot.png")
 
#driver.quit()
cs

 

 

38번 라인에서 캡쳐한 'screenshot.png'

 

 

※ Selenium Documentation

 

반응형
Posted by J-sean
:
반응형

urllib와 beautifulsoup을 이용해 Naver에서 '이 시각 주요 뉴스' 목록을 가져온다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from bs4 import BeautifulSoup as bs
import urllib.request as req
 
url = 'https://news.naver.com/'
res = req.urlopen(url)
 
soup = bs(res, 'html.parser')
title1 = soup.find('h4''tit_h4').string # 'tit_h4 tit_main1'
# title1 = soup.find(attrs = {'class' : 'tit_h4 tit_main1'}).string
print('\t\tNAVER', title1, '\n'
 
print('-'*20'find_all()''-'*20)
headlines1 = soup.find_all('a''nclicks(hom.headcont)')
# headlines1 = soup.find_all(attrs = {'class' : 'nclicks(hom.headcont)'})
# The find_all() method looks through a tag’s descendants and
# retrieves all descendants that match your filters.
for i, news in enumerate(headlines1):
    #if news.string == None:
    #    print('%2d: None' %i)
    #    continue
    print('%2d: %s' %(i + 1, news.string))
 
print('-'*20'select()''-'*20)
headlines2 = soup.select('div.newsnow_tx_inner > a')
# Beautiful Soup supports the most commonly-used CSS selectors. Just pass a
# string into the .select() method of a Tag object or the BeautifulSoup object itself.
for i, news in enumerate(headlines2):
    #if news.string == None:
    #    print('%2d: None' %i)
    #    continue
    print('%2d: %s' %(i + 1, news.string))
cs



반응형
Posted by J-sean
:

tkinter GUI

OpenCV 2018. 12. 29. 10:06 |
반응형

OpenCV와 함께 tkinter를 이용해 아래처럼 GUI를 원하는대로 만들 수 있다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import sys
import tkinter as tk
from PIL import Image, ImageTk
# The ImageTk module contains support to create and modify Tkinter BitmapImage and PhotoImage objects from PIL images
 
main = tk.Tk()
main.title("OpenCV-tkinter")
 
cvFrame = tk.Frame(main)
# A frame is basically just a container for other widgets.
cvFrame.grid(row = 0, column = 0, padx = 10, pady = 10)
 
lbl1 = tk.Label(cvFrame)
lbl1.grid(row = 0, column = 0)
 
lbl2 = tk.Label(cvFrame)
lbl2.grid(row = 0, column = 1)
 
def ExitButton():
    sys.exit()
 
btn = tk.Button(cvFrame, text = "Exit", font = ('Arial''30''bold'), foreground = "Red", command = ExitButton) # (.., height = 2, width = 60, ..)
# fron - As a tuple whose first element is the font family, followed by a size (in points if positive, in pixels if negative), optionally followed by
# a string containing one or more of the style modifiers bold, italic, underline, and overstrike.
btn.grid(row = 1, column = 0, columnspan = 2, sticky = tk.N + tk.S + tk.W + tk.E)
# columnspan - Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and merge them into one
# larger cell by setting the columnspan option to the number of cells. For example, w.grid(row=0, column=2, columnspan=3) would place widget
# w in a cell that spans columns 2, 3, and 4 of row 0.
# sticky - This option determines how to distribute any extra space within the cell that is not taken up by the widget at its natural size.
 
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('frame_size =', frame_size)
 
def show_frame():
    retval, frame = cap.read()
    frame = cv2.flip(frame, 1)
 
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    # Creates an image memory from an object exporting the array interface (using the buffer protocol).
    imgtk = ImageTk.PhotoImage(image = img)
    # A Tkinter-compatible photo image. This can be used everywhere Tkinter expects an image object.
    # If the image is an RGBA image, pixels having alpha 0 are treated as transparent.
    lbl1.imgtk = imgtk
    lbl1.configure(image = imgtk)
    # Set the values of one or more options.
 
    lbl2.imgtk = imgtk
    lbl2.configure(image = imgtk)
 
    # lbl.after(10, show_frame)
    main.after(10, show_frame)
    # Requests Tkinter to call function callback with arguments args after a delay of at least delay_ms milliseconds.
 
show_frame()
main.mainloop()
cs


반응형
Posted by J-sean
:
반응형

Meanshift


The intuition behind the meanshift is simple. Consider you have a set of points. (It can be a pixel distribution like histogram backprojection). You are given a small window (may be a circle) and you have to move that window to the area of maximum pixel density (or maximum number of points). It is illustrated in the simple image given below:

Meanshift의 원리는 간단하다. 우선 어떤 공간에 많은 점들이 있다고 가정해 보자 (Histogram backprojection으로 분포된 픽셀일 수도 있다). 그 공간에 속이 빈 작은 창(또는 원)을 놓고 점의 밀도가 가장 높은 곳으로 그 창을 움직여야 한다 (가능한 많은 점들을 포함 하도록). 간단히 그림으로 표현하면 아래와 같다.

The initial window is shown in blue circle with the name “C1”. Its original center is marked in blue rectangle, named “C1_o”. But if you find the centroid of the points inside that window, you will get the point “C1_r” (marked in small blue circle) which is the real centroid of window. Surely they don’t match. So move your window such that circle of the new window matches with previous centroid. Again find the new centroid. Most probably, it won’t match. So move it again, and continue the iterations such that center of window and its centroid falls on the same location (or with a small desired error). So finally what you obtain is a window with maximum pixel distribution. It is marked with green circle, named “C2”. As you can see in image, it has maximum number of points. The whole process is demonstrated on a static image below:

파란창(원) C1의 처음 중심 위치는 파란 사각형의 "C1_o"이다. 하지만 이 원의 실제 무게중심(centroid)은 "C1_r"이다 (파란 원). 이 둘은 일치하지 않는다. 그럼 원의 중심이 무게 중심과 일치 하도록 원을 이동시켜 보자. 그리고 다시 원의 무게 중심을 찾는다. 아마 다시 일치 하지 않을 것이다. 다시 원을 이동시켜 원의 중심과 무게 중심이 일치하도록(혹은 무시할 만한 오차가 될 때 까지) 반복 한다. 결국 원 안 점들의 밀도가 최대가 되는 지점을 얻게 될 것이다. 이 지점은 녹색 원 "C2"로 표시 되었다. 그림에서 알 수 있듯이, 이 원에 가장 많은 점들이 들어가게 된다. 전체 과정은 아래 이미지에 표현 되어 있다.

So we normally pass the histogram backprojected image and initial target location. When the object moves, obviously the movement is reflected in histogram backprojected image. As a result, meanshift algorithm moves our window to the new location with maximum density.

일반적으로 histogram backprojected image와 초기 target의 위치를 알려 준다. 물체가 움직이면 histogram backprojected image에 움직임이 반영 되고 그 결과, meanshift 알고리즘이 밀도가 가장 높은 곳으로 원을 옮기게 된다.


Meanshift in OpenCV


To use meanshift in OpenCV, first we need to setup the target, find its histogram so that we can backproject the target on each frame for calculation of meanshift. We also need to provide initial location of window. For histogram, only Hue is considered here. Also, to avoid false values due to low light, low light values are discarded using cv2.inRange() function.

OpenCV에서 meanshift를 이용하기 위해서는 우선 target을 지정하고 target의 histogram을 계산해야 한다. 이 histogram으로 각 frame에서 target을 backproject하여 meanshift를 계산한다. 잘못된 값이 나오는걸 막기 위해 너무 낮은 light 값은 cv2.inRange()를 이용해 제거한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import cv2
 
cap = cv2.VideoCapture('slow.flv')
 
# take first frame of the video
ret,frame = cap.read()
 
# setup initial location of window
r,h,c,w = 250,90,400,125  # simply hardcoded the values
track_window = (c,r,w,h)
 
# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0.60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
 
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 101 )
 
while(1):
    ret ,frame = cap.read()
 
    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
 
        # apply meanshift to get the new location
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)
 
        # Draw it on image
        x,y,w,h = track_window
        img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
        cv2.imshow('img2',img2)
 
        k = cv2.waitKey(60& 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k)+".jpg",img2)
 
    else:
        break
 
cv2.destroyAllWindows()
cap.release()
cs


Three frames in a video I used is given below:

아래 사진들은 위 코드에 사용된 video에서 추출한 프레임들이다.



Camshift


Did you closely watch the last result? There is a problem. Our window always has the same size when car is farther away and it is very close to camera. That is not good. We need to adapt the window size with size and rotation of the target. Once again, the solution came from “OpenCV Labs” and it is called CAMshift (Continuously Adaptive Meanshift) published by Gary Bradsky in his paper “Computer Vision Face Tracking for Use in a Perceptual User Interface” in 1988.

위 코드의 결과를 확인하면 문제점을 찾을 수 있다. 자동차가 멀리 있거나 가까이 있는거에 상관 없이 창은 항상 일정한 크기이다. 바람직한 현상은 아니다. Target의 크기와 회전 상태에 따라 창의 크기도 변경되어야 한다. 이에 "OpenCV Labs"의 Gary Bradsky가 1988년 발표한 논문 "Computer Vision Face Tracking for Use in a Perceptual User Interface에서 CAMshift (Continuously Adaptive Meanshift)라는 해결책을 제시 하였다.

It applies meanshift first. Once meanshift converges, it updates the size of the window as, . It also calculates the orientation of best fitting ellipse to it. Again it applies the meanshift with new scaled search window and previous window location. The process is continued until required accuracy is met.

CAMshift는 우선 meanshift를 적용 한다. Meanshift가 한 점으로 수렴하면 창의 사이즈를 로 변경하고 target에 꼭 맞는 타원의 방향을 계산한다. 그리고 다시 크기가 변경된 search window와 이전 창의 위치를 이용해 meanshift를 적용한다. 이 과정은 충분히 정확한 결과를 얻을 때 까지 계속 된다.



Camshift in OpenCV


It is almost same as meanshift, but it returns a rotated rectangle (that is our result) and box parameters (used to be passed as search window in next iteration). See the code below:

Meanshift와 거의 비슷하지만 회전된 사각형과 box parameter(다음 iteration을 위해 search window 값으로 넘겨진다)를 반환 한다는 점이 다르다. 아래 코드를 보자.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import cv2
 
cap = cv2.VideoCapture('slow.flv')
 
# take first frame of the video
ret,frame = cap.read()
 
# setup initial location of window
r,h,c,w = 250,90,400,125  # simply hardcoded the values
track_window = (c,r,w,h)
 
# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0.60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
 
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 101 )
 
while(1):
    ret ,frame = cap.read()
 
    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
 
        # apply meanshift to get the new location
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)
 
        # Draw it on image
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame,[pts],True, 255,2)
        cv2.imshow('img2',img2)
 
        k = cv2.waitKey(60& 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k)+".jpg",img2)
 
    else:
        break
 
cv2.destroyAllWindows()
cap.release()
cs


Three frames of the result is shown below:

아래 사진들은 위 코드에 사용된 video에서 추출한 프레임들이다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import cv2
import numpy as np
 
roi = None
drag_start = None
mouse_status = 0
tracking_start = False
 
def onMouse(event, x, y, flags, param = None):
    global roi
    global drag_start
    global mouse_status
    global tracking_start
    
    if event == cv2.EVENT_LBUTTONDOWN:
        drag_start = (x, y)
        roi = (0000# ROI를 재설정하는 경우를 위한 초기화
        tracking_start = False
    elif event == cv2.EVENT_MOUSEMOVE:
        if flags == cv2.EVENT_FLAG_LBUTTON:
            xmin = min(x, drag_start[0])
            ymin = min(y, drag_start[1])
            xmax = max(x, drag_start[0])
            ymax = max(y, drag_start[1])
            roi = (xmin, ymin, xmax, ymax)
            mouse_status = 1
    elif event == cv2.EVENT_LBUTTONUP:
        mouse_status = 2
 
cv2.namedWindow('meanShift tracking')
cv2.setMouseCallback('meanShift tracking', onMouse)
 
cap = cv2.VideoCapture(0)
if(not cap.isOpened()):
    print('Error opening video')
height, width = (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
roi_mask = np.zeros((height, width), dtype = np.uint8)
term_crit = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 101)
 
while True:
    ret, meanframe = cap.read()
    if not ret:
        break
    
    camframe = meanframe.copy()
    hsv = cv2.cvtColor(meanframe, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, (0.60.32.), (180.255.255.))
    # Checks if array elements lie between the elements of two other arrays.
 
    if mouse_status == 1:
        x1, y1, x2, y2 = roi
        cv2.rectangle(meanframe, (x1, y1), (x2, y2), (25500), 2)
 
    if mouse_status == 2:
        print('Initializing...', end = ' ')
        mouse_status = 0
        x1, y1, x2, y2 = roi
        if (np.abs(x1 - x2) < 10or (np.abs(y1 - y2) < 10):
            print('failed. Too small ROI. (Width: %d, Height: %d)' %(np.abs(x1 - x2), np.abs(y1 - y2)))
            continue
 
        mask_roi = mask[y1:y2, x1:x2]
        hsv_roi = hsv[y1:y2, x1:x2]
 
        hist_roi = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0180])
        cv2.normalize(hist_roi, hist_roi, 0255, cv2.NORM_MINMAX)
        track_window1 = (x1, y1, x2 - x1, y2 - y1)
        track_window2 = (x1, y1, x2 - x1, y2 - y1)
        tracking_start = True
        print('Done.')
 
    if tracking_start:
        backP = cv2.calcBackProject([hsv], [0], hist_roi, [0180], 1)
        # Calculates the back projection of a histogram.
        backP &= mask
        cv2.imshow('backP', backP)
 
        ret, track_window1 = cv2.meanShift(backP, track_window1, term_crit)
        # Finds an object on a back projection image.
        x, y, w, h = track_window1
        cv2.rectangle(meanframe, (x, y), (x + w, y + h), (00255), 2)
 
        track_box, track_window2 = cv2.CamShift(backP, track_window2, term_crit)
        # Finds an object center, size, and orientation.
        x, y, w, h = track_window2
        cv2.rectangle(camframe, (x, y), (x + w, y + h), (02550), 2)
        cv2.ellipse(camframe, track_box, (0255255), 2)
        pts = cv2.boxPoints(track_box) # Finds the four vertices of a rotated rect.
        pts = np.int0(pts)
        dst = cv2.polylines(camframe, [pts], True, (00255), 2)
 
    cv2.imshow('meanShift tracking', meanframe)
    cv2.imshow('CamShift tracking', camframe)
    key = cv2.waitKey(25)
    if key == 27:
        break
 
if cap.isOpened():
    cap.release()
cv2.destroyAllWindows()
cs


반응형
Posted by J-sean
:
반응형

Detecting a semiconductor wafer by Hough transform.

원형인 반도체 Wafer를 감지 한다. 배경에 노이즈가 많으면 오감지 가능성이 커진다.


작성 중.............



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    VideoCapture cap(0);
 
    if (!cap.isOpened()) {
        cerr << "Camera open failed." << endl;
 
        return 0;
    }
    
    int frameWidth = (int)cap.get(CAP_PROP_FRAME_WIDTH);
    int frameHeight = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
 
    Mat frame, grayed, blurred;
    
    string textWafer = "Wafer detected";
    string textNoWafer = "No wafer detected";
    // Size cv::getTextSize(const String & text, int fontFace, double fontScale, int thickness,    int* baseLine)
    Size textSizeWafer = getTextSize(textWafer, FONT_HERSHEY_SIMPLEX, 12NULL);
    Size textSizeNoWafer = getTextSize(textNoWafer, FONT_HERSHEY_SIMPLEX, 12NULL);
 
    while (true) {
        cap >> frame;
        if (frame.empty())
            break;
        
        cvtColor(frame, grayed, COLOR_BGR2GRAY);
        blur(grayed, blurred, Size(33));
        //GaussianBlur(grayed, blurred, Size(), 1.0);
        
        vector<Vec3f> circles;
        // void cv::HoughCircles(InputArray image, OutputArray circles, int method, double dp, double minDist, double param1 = 100, double param2 = 100, int minRadius = 0, int maxRadius = 0)
        // param1 - First method-specific parameter. In case of HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny edge detector (the lower one is twice smaller).
        // param2 - Second method - specific parameter.In case of HOUGH_GRADIENT, it is the accumulator threshold for the circle centers at the detection stage.The smaller it is, the more
        //            false circles may be detected.Circles, corresponding to the larger accumulator values, will be returned first.
        HoughCircles(blurred, circles, HOUGH_GRADIENT, 13001006050);
 
        if (circles.empty()) {
            putText(frame, textNoWafer, Point(cvRound(frameWidth / 2 - textSizeNoWafer.width / 2), cvRound(frameHeight / 2 + textSizeNoWafer.height / 2)), FONT_HERSHEY_SIMPLEX, 1, Scalar(00255), 2);
            imshow("frame", frame);
 
            if (waitKey(10== 27)
                break;
 
            continue;
        }
 
        for (Vec3f c : circles) {
            Point center(cvRound(c[0]), cvRound(c[1]));
            int radius = cvRound(c[2]);
 
            circle(frame, center, radius, Scalar(00255), 2);
            circle(frame, center, 2, Scalar(25500), 2);
            // void cv::putText(InputOutputArray img, const String & text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false)
            putText(frame, textWafer, Point2i(center.x - textSizeWafer.width / 2, center.y + textSizeWafer.height / 2), FONT_HERSHEY_SIMPLEX, 1, Scalar(02550), 2);
            //cout << "Wafer radius: " << radius << endl;
        }
 
        imshow("frame", frame);
 
        if (waitKey(10== 27)
            break;
    }
    
    return 0;
}












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
 
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
 
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('frame_size =', frame_size)
 
text = 'Wafer detected'
textSize, baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 12)
 
while True:
    retval, frame = cap.read()
    if not retval:
        break
    blured = cv2.GaussianBlur(frame, ksize = (1515), sigmaX = 10.0)
    # Blurs an image using a Gaussian filter.
    grayed = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(grayed, method = cv2.HOUGH_GRADIENT, dp = 1, minDist = 300,
                            param2 = 15, minRadius = 50)
    # Finds circles in a grayscale image using the Hough transform.
    if circles is None:
        cv2.imshow('Wafer detection', frame)
        key = cv2.waitKey(25)
        if key == 27# ESC
            break
        continue
 
    for circle in circles[0, :]:    
        cx, cy, r = circle
        cv2.circle(frame, (cx, cy), r, (00255), 2)    # Wafer
        cv2.circle(frame, (cx, cy), 2, (25500), 2)    # Center
 
        textX = int(cx - textSize[0/ 2)
        textY = int(cy + textSize[1/ 2)
        cv2.putText(frame, text, (textX, textY), cv2.FONT_HERSHEY_SIMPLEX, 1, (02550), 2)
        
    cv2.imshow('Wafer detection', frame)
 
    key = cv2.waitKey(25)
    if key == 27# ESC
        break
 
 
if cap.isOpened():
    cap.release()
cv2.destroyAllWindows()
cs


반응형

'OpenCV' 카테고리의 다른 글

OpenCV Graphic User Interface(GUI) with cvui  (1) 2019.07.01
How to extract video from a video file 영상 추출 하기  (0) 2019.06.29
Video Analysis - Optical Flow  (0) 2019.01.03
tkinter GUI  (0) 2018.12.29
Video Analysis - Meanshift and CAMshift  (0) 2018.12.27
Posted by J-sean
:
반응형

Windows에서 Visual Studio를 이용해 Linux에 원격으로 접속해서 C++ 프로그램 개발이 가능하다.

Linux에는 미리 gcc나 g++가 설치 되어 있어야 한다.

- Fedora: sudo dnf install g++

- Ubuntu: sudo apt-get install g++


New Project 창을 열고 Visual C++ - Cross Platform - Linux - 원하는 프로젝트를 선택해서 이름과 디렉토리를 지정해 준다.


간단한 소스가 입력된 프로젝트가 생성 된다. 아직 Linux에 연결되지는 않았다.


ifconfig 명령으로 접속할 Linux system의 주소를 확인한다.




Tools - Options창에서 Cross Platform - Add를 클릭한다.


Host name, Port, User name, Authentication type, Password를 입력한다.


헤더 파일, 인텔리센스등의 업데이트가 진행 된다.


업데이트가 완료되면 연결된 Linux system의 정보가 등록 된다.


Build 하면 Source를 Linux system으로 복사하고 Remote Build가 진행 된다.


Linux system에서 확인해 보면 실행파일이 생성 되어 있고 문제 없이 실행 된다.


Property Pages에서 Output Directory, Remote Build Machine등 여러가지 설정을 바꿀 수 있다.


반응형
Posted by J-sean
:
반응형

아래와 같은 도로 이미지에서 차선 부분만 감지 한다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from PIL import Image
from PIL import ImageFilter
 
source = Image.open("road.jpg")
result = Image.new("RGB", source.size)
# Creates a new image with the given mode and size.
sx, sy = source.size
horizon = 550 # 550픽셀 아래에 도로가 있다고 가정.
 
for y in range(horizon, sy):
    for x in range(sx):
        if min(source.getpixel((x, y))) > 0xf0:
            result.putpixel((x, y), (0xff0xff0xff))
            # RGB값 중 최소값이 0xf0이상이면 흰색으로 변경
 
result = result.filter(ImageFilter.FIND_EDGES)
#result = result.filter(ImageFilter.BoxBlur(1))
# Filters this image using the given filter.
result.save("result.jpg")
# Saves this image under the given filename.
result.show()
cs


결과:

반응형
Posted by J-sean
:
반응형

'Pillow 이미지 서치(Image search) 1' 은 target과 source의 모든 픽셀이 정확히 일치하는 경우만 True로 판단 하기 때문에 PNG나 BMP같은 무손실 압축 그래픽 파일에만 적용 가능하다. JPEG같은 손실 압축 그래픽 파일은 target과 source의 오차를 감안해야 한다.

 

2018/11/30 - [Software/Python] - Pillow 이미지 서치(Image Search) 1

2019/07/08 - [Software/OpenCV] - Template Matching(Image Searching) - 부분 이미지 검색

2019/07/10 - [Software/OpenCV] - Template Matching(Image Searching) for multiple objects - 반복되는 이미지 모두 찾기

2019/07/12 - [Software/OpenCV] - Template Matching(Image Searching) with a mask for multiple objects - 마스크를 이용해 (배경이 다른) 반복되는 이미지 모두 찾기

 

Target: 93 X 47

 

Source: 600 X 600

 

강아지의 앞발에 위치한 타겟 위치를 찾아 보자.

 

Tolerance: 30

Step: 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from PIL import Image
from PIL import ImageDraw
from PIL import ImageChops
from PIL import ImageStat
import sys
import time
 
source = Image.open("source.jpg")
sx, sy = source.size
target = Image.open("target.jpg")
tx, ty = target.size
tolerance = 30 # 오차 범위는 30 정도면 적당한거 같다.
step = 2 # 모든 픽셀을 검사하면 너무 오랜 시간이 걸린다. 한 개 건너 한 개 픽셀만 검사.
 
print("Source size: ", source.size)
print("Target size: ", target.size)
 
trial = 0 # Image search 시도 횟수.
 
def Search(cx, cy, tolerance):
    compare = source.crop((cx, cy, cx + tx, cy + ty)) # 소스에서 타겟으로 판단되는 위치의 이미지를 타겟 사이즈 만큼 잘라낸다.
    # Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
    print("Compare size: ", compare.size)
 
    diff = ImageChops.difference(compare, target) # 타겟과 타겟으로 판단되는 부분의 픽셀값 비교.
    stat = ImageStat.Stat(diff)
    global trial
    if max(max(stat.extrema[0]), max(stat.extrema[1]), max(stat.extrema[2])) <= tolerance:
        print("Target found(Min, max): ", stat.extrema)
        return True
    else:
        trial += 1
        return False
 
draw = ImageDraw.Draw(source)    # Creates an object that can be used to draw in the given image.
start = time.time()
 
for y in range(sy - ty):                # 소스의 처음부터 타겟 사이즈를 뺀 위치 까지 전체 검색을 시작 한다.
    for x in range(0, sx - tx, step):    # 처음 (10 X 10)개 픽셀의 값이 비슷 하다면 Search()로 타겟 사이즈 전체를 다시 확인한다.
        compare = source.crop((x, y, x + 10, y + 10))
        partial_target = target.crop((001010))
        diff = ImageChops.difference(compare, partial_target) # 각 픽셀값 차의 절대값이 반환 된다.
        # Returns the absolute value of the pixel-by-pixel difference between the two images.
        stat = ImageStat.Stat(diff)
 
        if max(max(stat.extrema[0]), max(stat.extrema[1]), max(stat.extrema[2])) < tolerance:
            if Search(x, y, tolerance) == True:
                print("Top left point: (%d, %d)" %(x, y))
                print("Center of targe point: (%d, %d)" %(x + target.width / 2, y + target.height / 2))
                print("Number of total wrong detection: ", trial)
                draw.rectangle((x, y, x + target.width, y + target.height), outline = (25500))
                # Draws a rectangle. 소스 이미지의 타겟 부분에 빨간 사각형을 그린다.
                end = time.time()
                print("Seraching time: ", end - start)
                source.show()
                sys.exit()
            else:
                print("At (%d, %d): Target not found" %(x, y))
                print("Wrong detection count: ", trial)
 
end = time.time()
print("Image search failed.")
print("Seraching time: ", end - start)
 
 

 

소스를 입력하고 빌드한다.

 

 

결과

 

123번 잘못된 지점을 검색했고 (232, 497)위치의 Target을 찾는데 총 15.66초가 걸렸다.

JPEG파일의 손실 압축 때문에 Target과 Source의 픽셀이 최대 RGB(16, 9, 15)만큼 차이가 발생 했다.

Target을 찾지 못한다면 Step과 Tolerance 값을 적당히 수정해야 한다.

 

반응형
Posted by J-sean
: