import cv2
import ctypes
import numpy as np
import time
import serial

# NG출력 폰트, 문자크기, 두께 설정.
font = cv2.FONT_HERSHEY_COMPLEX  # normal size sans-serif font
fontScale = 5
thickness = 4

# 화면의 해상도와 크기 불러오기.
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
screen_x, screen_y = screen_width/2, screen_height/2
int(screen_x), int(screen_y)

print(screen_x, screen_y)

def nothing(x):
    pass



cap = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars", cv2.WINDOW_NORMAL)

################## 데이터 입력 #########################
# Rivet coordinates 리벳의 위치 좌표 정보 입력 (x, y 좌표)
#[100, 100], [200, 200], [300, 300], [400, 400], [100, 400], [100, 300]
Rivets_coord = [[100, 100], [200, 200]]

Rivet_num = len(Rivets_coord)                     # 리벳의 갯수값 저장.

Rivet_tuple = []                                  # 튜플값을 저장할 리스트
for i in range(Rivet_num):
    Rivet_tuple.append(tuple(Rivets_coord[i]))    # 리벳 좌표값을 튜플로 변환후 리스트에 저장.

#테스트 출력
print(Rivet_tuple)
print(Rivets_coord[0][0], Rivets_coord[0][1])

# FIND_BLACK
cv2.createTrackbar("graybar", "Trackbars", 196, 255, nothing)
cv2.createTrackbar("bluebar", "Trackbars", 136,  255, nothing)
cv2.createTrackbar("greenbar", "Trackbars", 96, 255, nothing)
cv2.createTrackbar("redbar", "Trackbars", 76,  255, nothing)
cv2.createTrackbar("hsv hbar", "Trackbars", 136, 255, nothing)
cv2.createTrackbar("hsv sbar", "Trackbars", 145, 255, nothing)
cv2.createTrackbar("hsv vbar", "Trackbars", 78, 255, nothing)
cv2.createTrackbar("hsl hbar", "Trackbars", 127, 255, nothing)
cv2.createTrackbar("hsl sbar", "Trackbars", 107, 255, nothing) # 75
cv2.createTrackbar("hsl lbar", "Trackbars", 255, 255, nothing)

cv2.createTrackbar("bluebar_", "Trackbars", 18,  255, nothing) # 0
cv2.createTrackbar("greenbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("redbar_", "Trackbars", 0,  255, nothing)
cv2.createTrackbar("hsv hbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsv sbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsv vbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl hbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl sbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl lbar_", "Trackbars", 0, 255, nothing)

while True:
    _, frame = cap.read()

    # col,row,_ = frame.shape # 해상도
    # print(col,row)
    frame2 = frame.copy() # 영상원본

    frame = cv2.GaussianBlur(frame, (3, 3), 0)              # 원본에 가우시안 필터적용
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    blue, green, red = cv2.split(frame)                     # 원본에서 BGR 분리
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)      # HSV(색상, 채도, 명도) 분리
    h, s, v = cv2.split(frame_hsv)                          # 분리후 저장.

    frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)      # BGR -> HLS로
    H, L, S = cv2.split(frame_hls)                          # H,L,S 분리

    gray_c = cv2.getTrackbarPos("graybar", "Trackbars")         # 이하 필터값 튜닝 트렉바
    blue_c = cv2.getTrackbarPos("bluebar", "Trackbars")
    green_c = cv2.getTrackbarPos("greenbar", "Trackbars")
    red_c = cv2.getTrackbarPos("redbar", "Trackbars")
    hsv_h_c = cv2.getTrackbarPos("hsv hbar", "Trackbars")
    hsv_s_c = cv2.getTrackbarPos("hsv sbar", "Trackbars")
    hsv_v_c = cv2.getTrackbarPos("hsv vbar", "Trackbars")
    hsl_h_c = cv2.getTrackbarPos("hsl hbar", "Trackbars")
    hsl_s_c = cv2.getTrackbarPos("hsl sbar", "Trackbars")
    hsl_l_c = cv2.getTrackbarPos("hsl lbar", "Trackbars")

    gray_c_ = cv2.getTrackbarPos("graybar_", "Trackbars")
    blue_c_ = cv2.getTrackbarPos("bluebar_", "Trackbars")
    green_c_ = cv2.getTrackbarPos("greenbar_", "Trackbars")
    red_c_ = cv2.getTrackbarPos("redbar_", "Trackbars")
    hsv_h_c_ = cv2.getTrackbarPos("hsv hbar_", "Trackbars")
    hsv_s_c_ = cv2.getTrackbarPos("hsv sbar_", "Trackbars")
    hsv_v_c_ = cv2.getTrackbarPos("hsv vbar_", "Trackbars")
    hsl_h_c_ = cv2.getTrackbarPos("hsl hbar_", "Trackbars")
    hsl_s_c_ = cv2.getTrackbarPos("hsl sbar_", "Trackbars")
    hsl_l_c_ = cv2.getTrackbarPos("hsl lbar_", "Trackbars")

    _, gray1 = cv2.threshold(gray_frame,gray_c,255,cv2.THRESH_BINARY_INV)
    _, blue1 = cv2.threshold(blue, blue_c, 255, cv2.THRESH_BINARY_INV)
    _, green1 = cv2.threshold(green, green_c, 255, cv2.THRESH_BINARY_INV)
    _, red1   = cv2.threshold(red, red_c, 255, cv2.THRESH_BINARY_INV)
    _, h1 = cv2.threshold(h, hsv_h_c, 255, cv2.THRESH_BINARY_INV)
    _, s1 = cv2.threshold(s, hsv_s_c, 255, cv2.THRESH_BINARY_INV)
    _, v1 = cv2.threshold(v, hsv_v_c, 255, cv2.THRESH_BINARY_INV)
    _, H1 = cv2.threshold(H, hsl_h_c, 255, cv2.THRESH_BINARY_INV)
    _, L1 = cv2.threshold(L, hsl_s_c, 255, cv2.THRESH_BINARY_INV)
    _, S1 = cv2.threshold(S, hsl_l_c, 255, cv2.THRESH_BINARY_INV)

    _, gray_ = cv2.threshold(gray_frame, gray_c_, 255, cv2.THRESH_BINARY)
    _, blue_ = cv2.threshold(blue, blue_c_, 255, cv2.THRESH_BINARY)
    _, green_ = cv2.threshold(green, green_c_, 255, cv2.THRESH_BINARY)
    _, red_ = cv2.threshold(red, red_c_, 255, cv2.THRESH_BINARY)
    _, h_ = cv2.threshold(h, hsv_h_c_, 255, cv2.THRESH_BINARY)
    _, s_ = cv2.threshold(s, hsv_s_c_, 255, cv2.THRESH_BINARY)
    _, v_ = cv2.threshold(v, hsv_v_c_, 255, cv2.THRESH_BINARY)
    _, H_ = cv2.threshold(H, hsl_h_c_, 255, cv2.THRESH_BINARY)
    _, L_ = cv2.threshold(L, hsl_s_c_, 255, cv2.THRESH_BINARY)
    _, S_ = cv2.threshold(S, hsl_l_c_, 255, cv2.THRESH_BINARY)

    final_mask = gray1                                              # 하나씩 필터 mask를 씌움.
    final_mask = cv2.bitwise_and(final_mask, blue1)
    final_mask = cv2.bitwise_and(final_mask, green1)
    final_mask = cv2.bitwise_and(final_mask, red1)
    final_mask = cv2.bitwise_and(final_mask, h1)
    final_mask = cv2.bitwise_and(final_mask, s1)
    final_mask = cv2.bitwise_and(final_mask, v1)
    final_mask = cv2.bitwise_and(final_mask, H1)
    final_mask = cv2.bitwise_and(final_mask, L1)
    final_mask = cv2.bitwise_and(final_mask, S1)

    final_mask = cv2.bitwise_and(final_mask, gray_)
    final_mask = cv2.bitwise_and(final_mask, blue_)
    final_mask = cv2.bitwise_and(final_mask, green_)
    final_mask = cv2.bitwise_and(final_mask, red_)
    final_mask = cv2.bitwise_and(final_mask, v_)

    result = cv2.bitwise_and(frame2, frame2, mask=final_mask)       # 필터 결과 저장

    # _, contours, _ = cv2.findContours(final_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)  # 컨투어 찾기
    # if len(contours) != 0:
    #     for contour in contours:
    #         if (cv2.contourArea(contour) > 800) and (cv2.contourArea(contour) < 4500):  # **필요한 면적을 찾아 중심점 표시
    #             ball_area = cv2.contourArea(contour)
    #             mom = contour
    #             M = cv2.moments(mom)
    #             cx_origin = int(M['m10'] / M['m00'])
    #             cy_origin = int(M['m01'] / M['m00'])
    #             cv2.circle(frame, (cx_origin, cy_origin), 10, (0, 255, 0), -1) # Center of puck
    #             print(cx_origin, cy_origin) # 중심점값읽기

    reverse = cv2.bitwise_not(final_mask)
    reverse_copy = reverse.copy()

    # ** 리벳을 검출할 위치에 원으로 좌표 표시.
    for i in range(Rivet_num):
        reverse_copy = cv2.circle(reverse_copy, Rivet_tuple[i], 10, (0, 0, 255), -1)  # 가운데 점 픽셀값 확인용 (x,y)값으로 받음.
        frame = cv2.circle(frame, Rivet_tuple[i], 10, (0, 255, 0), -1)                # 원본에도 점 표시.

    # ** 한 픽셀당 Binary 값을 표시.
    # [y , x]의 픽셀값 입력받음.
    pixel_val_list = []
    for i in range(Rivet_num):
        pixel_val = reverse[Rivets_coord[i][1], Rivets_coord[i][0]]     # 픽셀값 저장 (0, 255)
        if pixel_val == 255:                                            # 검출된곳은 1, 검출되지 않을곳은 0으로 변환.
            pixel_val = 0
        else:
            pixel_val = 1

        pixel_val_list.append(pixel_val)                                # 변환된 값을 리스트에 추가
        pixel_sum = sum(pixel_val_list)                                 # 모든 픽셀의 합

    print(pixel_val_list, pixel_sum)                                    # 픽셀값과 합계 출력

    if pixel_sum == Rivet_num:
        # 리벳의 갯수와 픽셀의 값이 일치하면 합격
        pass
    else:
        # 그 외 불합격
        cv2.putText(frame, '**NG**', (50, 300), font, fontScale, (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow('Frame', frame)                      # 원본
    cv2.imshow('result', final_mask)                # 필터링후
    cv2.imshow('reverse', reverse)                  # 반전 (픽셀값을 찍어보기 위해 흑백)
    cv2.imshow('location_check', reverse_copy)      # 리벳위치 픽셀체크 위치 확인용

    if cv2.waitKey(1) & 0xff == ord('q'):
        break
e2133ff15398301f5c8d991b866ec947cb17deb3
<mask token>


def nothing(x):
    pass


<mask token>

<mask token>
int(screen_x), int(screen_y)
print(screen_x, screen_y)


def nothing(x):
    pass


<mask token>
cv2.namedWindow('Trackbars', cv2.WINDOW_NORMAL)
<mask token>
for i in range(Rivet_num):
    Rivet_tuple.append(tuple(Rivets_coord[i]))
print(Rivet_tuple)
print(Rivets_coord[0][0], Rivets_coord[0][1])
cv2.createTrackbar('graybar', 'Trackbars', 196, 255, nothing)
cv2.createTrackbar('bluebar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('greenbar', 'Trackbars', 96, 255, nothing)
cv2.createTrackbar('redbar', 'Trackbars', 76, 255, nothing)
cv2.createTrackbar('hsv hbar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('hsv sbar', 'Trackbars', 145, 255, nothing)
cv2.createTrackbar('hsv vbar', 'Trackbars', 78, 255, nothing)
cv2.createTrackbar('hsl hbar', 'Trackbars', 127, 255, nothing)
cv2.createTrackbar('hsl sbar', 'Trackbars', 107, 255, nothing)
cv2.createTrackbar('hsl lbar', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('bluebar_', 'Trackbars', 18, 255, nothing)
cv2.createTrackbar('greenbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('redbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv vbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl lbar_', 'Trackbars', 0, 255, nothing)
while True:
    _, frame = cap.read()
    frame2 = frame.copy()
    frame = cv2.GaussianBlur(frame, (3, 3), 0)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blue, green, red = cv2.split(frame)
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(frame_hsv)
    frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
    H, L, S = cv2.split(frame_hls)
    gray_c = cv2.getTrackbarPos('graybar', 'Trackbars')
    blue_c = cv2.getTrackbarPos('bluebar', 'Trackbars')
    green_c = cv2.getTrackbarPos('greenbar', 'Trackbars')
    red_c = cv2.getTrackbarPos('redbar', 'Trackbars')
    hsv_h_c = cv2.getTrackbarPos('hsv hbar', 'Trackbars')
    hsv_s_c = cv2.getTrackbarPos('hsv sbar', 'Trackbars')
    hsv_v_c = cv2.getTrackbarPos('hsv vbar', 'Trackbars')
    hsl_h_c = cv2.getTrackbarPos('hsl hbar', 'Trackbars')
    hsl_s_c = cv2.getTrackbarPos('hsl sbar', 'Trackbars')
    hsl_l_c = cv2.getTrackbarPos('hsl lbar', 'Trackbars')
    gray_c_ = cv2.getTrackbarPos('graybar_', 'Trackbars')
    blue_c_ = cv2.getTrackbarPos('bluebar_', 'Trackbars')
    green_c_ = cv2.getTrackbarPos('greenbar_', 'Trackbars')
    red_c_ = cv2.getTrackbarPos('redbar_', 'Trackbars')
    hsv_h_c_ = cv2.getTrackbarPos('hsv hbar_', 'Trackbars')
    hsv_s_c_ = cv2.getTrackbarPos('hsv sbar_', 'Trackbars')
    hsv_v_c_ = cv2.getTrackbarPos('hsv vbar_', 'Trackbars')
    hsl_h_c_ = cv2.getTrackbarPos('hsl hbar_', 'Trackbars')
    hsl_s_c_ = cv2.getTrackbarPos('hsl sbar_', 'Trackbars')
    hsl_l_c_ = cv2.getTrackbarPos('hsl lbar_', 'Trackbars')
    _, gray1 = cv2.threshold(gray_frame, gray_c, 255, cv2.THRESH_BINARY_INV)
    _, blue1 = cv2.threshold(blue, blue_c, 255, cv2.THRESH_BINARY_INV)
    _, green1 = cv2.threshold(green, green_c, 255, cv2.THRESH_BINARY_INV)
    _, red1 = cv2.threshold(red, red_c, 255, cv2.THRESH_BINARY_INV)
    _, h1 = cv2.threshold(h, hsv_h_c, 255, cv2.THRESH_BINARY_INV)
    _, s1 = cv2.threshold(s, hsv_s_c, 255, cv2.THRESH_BINARY_INV)
    _, v1 = cv2.threshold(v, hsv_v_c, 255, cv2.THRESH_BINARY_INV)
    _, H1 = cv2.threshold(H, hsl_h_c, 255, cv2.THRESH_BINARY_INV)
    _, L1 = cv2.threshold(L, hsl_s_c, 255, cv2.THRESH_BINARY_INV)
    _, S1 = cv2.threshold(S, hsl_l_c, 255, cv2.THRESH_BINARY_INV)
    _, gray_ = cv2.threshold(gray_frame, gray_c_, 255, cv2.THRESH_BINARY)
    _, blue_ = cv2.threshold(blue, blue_c_, 255, cv2.THRESH_BINARY)
    _, green_ = cv2.threshold(green, green_c_, 255, cv2.THRESH_BINARY)
    _, red_ = cv2.threshold(red, red_c_, 255, cv2.THRESH_BINARY)
    _, h_ = cv2.threshold(h, hsv_h_c_, 255, cv2.THRESH_BINARY)
    _, s_ = cv2.threshold(s, hsv_s_c_, 255, cv2.THRESH_BINARY)
    _, v_ = cv2.threshold(v, hsv_v_c_, 255, cv2.THRESH_BINARY)
    _, H_ = cv2.threshold(H, hsl_h_c_, 255, cv2.THRESH_BINARY)
    _, L_ = cv2.threshold(L, hsl_s_c_, 255, cv2.THRESH_BINARY)
    _, S_ = cv2.threshold(S, hsl_l_c_, 255, cv2.THRESH_BINARY)
    final_mask = gray1
    final_mask = cv2.bitwise_and(final_mask, blue1)
    final_mask = cv2.bitwise_and(final_mask, green1)
    final_mask = cv2.bitwise_and(final_mask, red1)
    final_mask = cv2.bitwise_and(final_mask, h1)
    final_mask = cv2.bitwise_and(final_mask, s1)
    final_mask = cv2.bitwise_and(final_mask, v1)
    final_mask = cv2.bitwise_and(final_mask, H1)
    final_mask = cv2.bitwise_and(final_mask, L1)
    final_mask = cv2.bitwise_and(final_mask, S1)
    final_mask = cv2.bitwise_and(final_mask, gray_)
    final_mask = cv2.bitwise_and(final_mask, blue_)
    final_mask = cv2.bitwise_and(final_mask, green_)
    final_mask = cv2.bitwise_and(final_mask, red_)
    final_mask = cv2.bitwise_and(final_mask, v_)
    result = cv2.bitwise_and(frame2, frame2, mask=final_mask)
    reverse = cv2.bitwise_not(final_mask)
    reverse_copy = reverse.copy()
    for i in range(Rivet_num):
        reverse_copy = cv2.circle(reverse_copy, Rivet_tuple[i], 10, (0, 0, 
            255), -1)
        frame = cv2.circle(frame, Rivet_tuple[i], 10, (0, 255, 0), -1)
    pixel_val_list = []
    for i in range(Rivet_num):
        pixel_val = reverse[Rivets_coord[i][1], Rivets_coord[i][0]]
        if pixel_val == 255:
            pixel_val = 0
        else:
            pixel_val = 1
        pixel_val_list.append(pixel_val)
        pixel_sum = sum(pixel_val_list)
    print(pixel_val_list, pixel_sum)
    if pixel_sum == Rivet_num:
        pass
    else:
        cv2.putText(frame, '**NG**', (50, 300), font, fontScale, (0, 0, 255
            ), 2, cv2.LINE_AA)
    cv2.imshow('Frame', frame)
    cv2.imshow('result', final_mask)
    cv2.imshow('reverse', reverse)
    cv2.imshow('location_check', reverse_copy)
    if cv2.waitKey(1) & 255 == ord('q'):
        break

<mask token>
font = cv2.FONT_HERSHEY_COMPLEX
fontScale = 5
thickness = 4
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
screen_x, screen_y = screen_width / 2, screen_height / 2
int(screen_x), int(screen_y)
print(screen_x, screen_y)


def nothing(x):
    pass


cap = cv2.VideoCapture(0)
cv2.namedWindow('Trackbars', cv2.WINDOW_NORMAL)
Rivets_coord = [[100, 100], [200, 200]]
Rivet_num = len(Rivets_coord)
Rivet_tuple = []
for i in range(Rivet_num):
    Rivet_tuple.append(tuple(Rivets_coord[i]))
print(Rivet_tuple)
print(Rivets_coord[0][0], Rivets_coord[0][1])
cv2.createTrackbar('graybar', 'Trackbars', 196, 255, nothing)
cv2.createTrackbar('bluebar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('greenbar', 'Trackbars', 96, 255, nothing)
cv2.createTrackbar('redbar', 'Trackbars', 76, 255, nothing)
cv2.createTrackbar('hsv hbar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('hsv sbar', 'Trackbars', 145, 255, nothing)
cv2.createTrackbar('hsv vbar', 'Trackbars', 78, 255, nothing)
cv2.createTrackbar('hsl hbar', 'Trackbars', 127, 255, nothing)
cv2.createTrackbar('hsl sbar', 'Trackbars', 107, 255, nothing)
cv2.createTrackbar('hsl lbar', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('bluebar_', 'Trackbars', 18, 255, nothing)
cv2.createTrackbar('greenbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('redbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv vbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl lbar_', 'Trackbars', 0, 255, nothing)
while True:
    _, frame = cap.read()
    frame2 = frame.copy()
    frame = cv2.GaussianBlur(frame, (3, 3), 0)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blue, green, red = cv2.split(frame)
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(frame_hsv)
    frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
    H, L, S = cv2.split(frame_hls)
    gray_c = cv2.getTrackbarPos('graybar', 'Trackbars')
    blue_c = cv2.getTrackbarPos('bluebar', 'Trackbars')
    green_c = cv2.getTrackbarPos('greenbar', 'Trackbars')
    red_c = cv2.getTrackbarPos('redbar', 'Trackbars')
    hsv_h_c = cv2.getTrackbarPos('hsv hbar', 'Trackbars')
    hsv_s_c = cv2.getTrackbarPos('hsv sbar', 'Trackbars')
    hsv_v_c = cv2.getTrackbarPos('hsv vbar', 'Trackbars')
    hsl_h_c = cv2.getTrackbarPos('hsl hbar', 'Trackbars')
    hsl_s_c = cv2.getTrackbarPos('hsl sbar', 'Trackbars')
    hsl_l_c = cv2.getTrackbarPos('hsl lbar', 'Trackbars')
    gray_c_ = cv2.getTrackbarPos('graybar_', 'Trackbars')
    blue_c_ = cv2.getTrackbarPos('bluebar_', 'Trackbars')
    green_c_ = cv2.getTrackbarPos('greenbar_', 'Trackbars')
    red_c_ = cv2.getTrackbarPos('redbar_', 'Trackbars')
    hsv_h_c_ = cv2.getTrackbarPos('hsv hbar_', 'Trackbars')
    hsv_s_c_ = cv2.getTrackbarPos('hsv sbar_', 'Trackbars')
    hsv_v_c_ = cv2.getTrackbarPos('hsv vbar_', 'Trackbars')
    hsl_h_c_ = cv2.getTrackbarPos('hsl hbar_', 'Trackbars')
    hsl_s_c_ = cv2.getTrackbarPos('hsl sbar_', 'Trackbars')
    hsl_l_c_ = cv2.getTrackbarPos('hsl lbar_', 'Trackbars')
    _, gray1 = cv2.threshold(gray_frame, gray_c, 255, cv2.THRESH_BINARY_INV)
    _, blue1 = cv2.threshold(blue, blue_c, 255, cv2.THRESH_BINARY_INV)
    _, green1 = cv2.threshold(green, green_c, 255, cv2.THRESH_BINARY_INV)
    _, red1 = cv2.threshold(red, red_c, 255, cv2.THRESH_BINARY_INV)
    _, h1 = cv2.threshold(h, hsv_h_c, 255, cv2.THRESH_BINARY_INV)
    _, s1 = cv2.threshold(s, hsv_s_c, 255, cv2.THRESH_BINARY_INV)
    _, v1 = cv2.threshold(v, hsv_v_c, 255, cv2.THRESH_BINARY_INV)
    _, H1 = cv2.threshold(H, hsl_h_c, 255, cv2.THRESH_BINARY_INV)
    _, L1 = cv2.threshold(L, hsl_s_c, 255, cv2.THRESH_BINARY_INV)
    _, S1 = cv2.threshold(S, hsl_l_c, 255, cv2.THRESH_BINARY_INV)
    _, gray_ = cv2.threshold(gray_frame, gray_c_, 255, cv2.THRESH_BINARY)
    _, blue_ = cv2.threshold(blue, blue_c_, 255, cv2.THRESH_BINARY)
    _, green_ = cv2.threshold(green, green_c_, 255, cv2.THRESH_BINARY)
    _, red_ = cv2.threshold(red, red_c_, 255, cv2.THRESH_BINARY)
    _, h_ = cv2.threshold(h, hsv_h_c_, 255, cv2.THRESH_BINARY)
    _, s_ = cv2.threshold(s, hsv_s_c_, 255, cv2.THRESH_BINARY)
    _, v_ = cv2.threshold(v, hsv_v_c_, 255, cv2.THRESH_BINARY)
    _, H_ = cv2.threshold(H, hsl_h_c_, 255, cv2.THRESH_BINARY)
    _, L_ = cv2.threshold(L, hsl_s_c_, 255, cv2.THRESH_BINARY)
    _, S_ = cv2.threshold(S, hsl_l_c_, 255, cv2.THRESH_BINARY)
    final_mask = gray1
    final_mask = cv2.bitwise_and(final_mask, blue1)
    final_mask = cv2.bitwise_and(final_mask, green1)
    final_mask = cv2.bitwise_and(final_mask, red1)
    final_mask = cv2.bitwise_and(final_mask, h1)
    final_mask = cv2.bitwise_and(final_mask, s1)
    final_mask = cv2.bitwise_and(final_mask, v1)
    final_mask = cv2.bitwise_and(final_mask, H1)
    final_mask = cv2.bitwise_and(final_mask, L1)
    final_mask = cv2.bitwise_and(final_mask, S1)
    final_mask = cv2.bitwise_and(final_mask, gray_)
    final_mask = cv2.bitwise_and(final_mask, blue_)
    final_mask = cv2.bitwise_and(final_mask, green_)
    final_mask = cv2.bitwise_and(final_mask, red_)
    final_mask = cv2.bitwise_and(final_mask, v_)
    result = cv2.bitwise_and(frame2, frame2, mask=final_mask)
    reverse = cv2.bitwise_not(final_mask)
    reverse_copy = reverse.copy()
    for i in range(Rivet_num):
        reverse_copy = cv2.circle(reverse_copy, Rivet_tuple[i], 10, (0, 0, 
            255), -1)
        frame = cv2.circle(frame, Rivet_tuple[i], 10, (0, 255, 0), -1)
    pixel_val_list = []
    for i in range(Rivet_num):
        pixel_val = reverse[Rivets_coord[i][1], Rivets_coord[i][0]]
        if pixel_val == 255:
            pixel_val = 0
        else:
            pixel_val = 1
        pixel_val_list.append(pixel_val)
        pixel_sum = sum(pixel_val_list)
    print(pixel_val_list, pixel_sum)
    if pixel_sum == Rivet_num:
        pass
    else:
        cv2.putText(frame, '**NG**', (50, 300), font, fontScale, (0, 0, 255
            ), 2, cv2.LINE_AA)
    cv2.imshow('Frame', frame)
    cv2.imshow('result', final_mask)
    cv2.imshow('reverse', reverse)
    cv2.imshow('location_check', reverse_copy)
    if cv2.waitKey(1) & 255 == ord('q'):
        break

import cv2
import ctypes
import numpy as np
import time
import serial
font = cv2.FONT_HERSHEY_COMPLEX
fontScale = 5
thickness = 4
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
screen_x, screen_y = screen_width / 2, screen_height / 2
int(screen_x), int(screen_y)
print(screen_x, screen_y)


def nothing(x):
    pass


cap = cv2.VideoCapture(0)
cv2.namedWindow('Trackbars', cv2.WINDOW_NORMAL)
Rivets_coord = [[100, 100], [200, 200]]
Rivet_num = len(Rivets_coord)
Rivet_tuple = []
for i in range(Rivet_num):
    Rivet_tuple.append(tuple(Rivets_coord[i]))
print(Rivet_tuple)
print(Rivets_coord[0][0], Rivets_coord[0][1])
cv2.createTrackbar('graybar', 'Trackbars', 196, 255, nothing)
cv2.createTrackbar('bluebar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('greenbar', 'Trackbars', 96, 255, nothing)
cv2.createTrackbar('redbar', 'Trackbars', 76, 255, nothing)
cv2.createTrackbar('hsv hbar', 'Trackbars', 136, 255, nothing)
cv2.createTrackbar('hsv sbar', 'Trackbars', 145, 255, nothing)
cv2.createTrackbar('hsv vbar', 'Trackbars', 78, 255, nothing)
cv2.createTrackbar('hsl hbar', 'Trackbars', 127, 255, nothing)
cv2.createTrackbar('hsl sbar', 'Trackbars', 107, 255, nothing)
cv2.createTrackbar('hsl lbar', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('bluebar_', 'Trackbars', 18, 255, nothing)
cv2.createTrackbar('greenbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('redbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsv vbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl hbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl sbar_', 'Trackbars', 0, 255, nothing)
cv2.createTrackbar('hsl lbar_', 'Trackbars', 0, 255, nothing)
while True:
    _, frame = cap.read()
    frame2 = frame.copy()
    frame = cv2.GaussianBlur(frame, (3, 3), 0)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blue, green, red = cv2.split(frame)
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(frame_hsv)
    frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
    H, L, S = cv2.split(frame_hls)
    gray_c = cv2.getTrackbarPos('graybar', 'Trackbars')
    blue_c = cv2.getTrackbarPos('bluebar', 'Trackbars')
    green_c = cv2.getTrackbarPos('greenbar', 'Trackbars')
    red_c = cv2.getTrackbarPos('redbar', 'Trackbars')
    hsv_h_c = cv2.getTrackbarPos('hsv hbar', 'Trackbars')
    hsv_s_c = cv2.getTrackbarPos('hsv sbar', 'Trackbars')
    hsv_v_c = cv2.getTrackbarPos('hsv vbar', 'Trackbars')
    hsl_h_c = cv2.getTrackbarPos('hsl hbar', 'Trackbars')
    hsl_s_c = cv2.getTrackbarPos('hsl sbar', 'Trackbars')
    hsl_l_c = cv2.getTrackbarPos('hsl lbar', 'Trackbars')
    gray_c_ = cv2.getTrackbarPos('graybar_', 'Trackbars')
    blue_c_ = cv2.getTrackbarPos('bluebar_', 'Trackbars')
    green_c_ = cv2.getTrackbarPos('greenbar_', 'Trackbars')
    red_c_ = cv2.getTrackbarPos('redbar_', 'Trackbars')
    hsv_h_c_ = cv2.getTrackbarPos('hsv hbar_', 'Trackbars')
    hsv_s_c_ = cv2.getTrackbarPos('hsv sbar_', 'Trackbars')
    hsv_v_c_ = cv2.getTrackbarPos('hsv vbar_', 'Trackbars')
    hsl_h_c_ = cv2.getTrackbarPos('hsl hbar_', 'Trackbars')
    hsl_s_c_ = cv2.getTrackbarPos('hsl sbar_', 'Trackbars')
    hsl_l_c_ = cv2.getTrackbarPos('hsl lbar_', 'Trackbars')
    _, gray1 = cv2.threshold(gray_frame, gray_c, 255, cv2.THRESH_BINARY_INV)
    _, blue1 = cv2.threshold(blue, blue_c, 255, cv2.THRESH_BINARY_INV)
    _, green1 = cv2.threshold(green, green_c, 255, cv2.THRESH_BINARY_INV)
    _, red1 = cv2.threshold(red, red_c, 255, cv2.THRESH_BINARY_INV)
    _, h1 = cv2.threshold(h, hsv_h_c, 255, cv2.THRESH_BINARY_INV)
    _, s1 = cv2.threshold(s, hsv_s_c, 255, cv2.THRESH_BINARY_INV)
    _, v1 = cv2.threshold(v, hsv_v_c, 255, cv2.THRESH_BINARY_INV)
    _, H1 = cv2.threshold(H, hsl_h_c, 255, cv2.THRESH_BINARY_INV)
    _, L1 = cv2.threshold(L, hsl_s_c, 255, cv2.THRESH_BINARY_INV)
    _, S1 = cv2.threshold(S, hsl_l_c, 255, cv2.THRESH_BINARY_INV)
    _, gray_ = cv2.threshold(gray_frame, gray_c_, 255, cv2.THRESH_BINARY)
    _, blue_ = cv2.threshold(blue, blue_c_, 255, cv2.THRESH_BINARY)
    _, green_ = cv2.threshold(green, green_c_, 255, cv2.THRESH_BINARY)
    _, red_ = cv2.threshold(red, red_c_, 255, cv2.THRESH_BINARY)
    _, h_ = cv2.threshold(h, hsv_h_c_, 255, cv2.THRESH_BINARY)
    _, s_ = cv2.threshold(s, hsv_s_c_, 255, cv2.THRESH_BINARY)
    _, v_ = cv2.threshold(v, hsv_v_c_, 255, cv2.THRESH_BINARY)
    _, H_ = cv2.threshold(H, hsl_h_c_, 255, cv2.THRESH_BINARY)
    _, L_ = cv2.threshold(L, hsl_s_c_, 255, cv2.THRESH_BINARY)
    _, S_ = cv2.threshold(S, hsl_l_c_, 255, cv2.THRESH_BINARY)
    final_mask = gray1
    final_mask = cv2.bitwise_and(final_mask, blue1)
    final_mask = cv2.bitwise_and(final_mask, green1)
    final_mask = cv2.bitwise_and(final_mask, red1)
    final_mask = cv2.bitwise_and(final_mask, h1)
    final_mask = cv2.bitwise_and(final_mask, s1)
    final_mask = cv2.bitwise_and(final_mask, v1)
    final_mask = cv2.bitwise_and(final_mask, H1)
    final_mask = cv2.bitwise_and(final_mask, L1)
    final_mask = cv2.bitwise_and(final_mask, S1)
    final_mask = cv2.bitwise_and(final_mask, gray_)
    final_mask = cv2.bitwise_and(final_mask, blue_)
    final_mask = cv2.bitwise_and(final_mask, green_)
    final_mask = cv2.bitwise_and(final_mask, red_)
    final_mask = cv2.bitwise_and(final_mask, v_)
    result = cv2.bitwise_and(frame2, frame2, mask=final_mask)
    reverse = cv2.bitwise_not(final_mask)
    reverse_copy = reverse.copy()
    for i in range(Rivet_num):
        reverse_copy = cv2.circle(reverse_copy, Rivet_tuple[i], 10, (0, 0, 
            255), -1)
        frame = cv2.circle(frame, Rivet_tuple[i], 10, (0, 255, 0), -1)
    pixel_val_list = []
    for i in range(Rivet_num):
        pixel_val = reverse[Rivets_coord[i][1], Rivets_coord[i][0]]
        if pixel_val == 255:
            pixel_val = 0
        else:
            pixel_val = 1
        pixel_val_list.append(pixel_val)
        pixel_sum = sum(pixel_val_list)
    print(pixel_val_list, pixel_sum)
    if pixel_sum == Rivet_num:
        pass
    else:
        cv2.putText(frame, '**NG**', (50, 300), font, fontScale, (0, 0, 255
            ), 2, cv2.LINE_AA)
    cv2.imshow('Frame', frame)
    cv2.imshow('result', final_mask)
    cv2.imshow('reverse', reverse)
    cv2.imshow('location_check', reverse_copy)
    if cv2.waitKey(1) & 255 == ord('q'):
        break

import cv2
import ctypes
import numpy as np
import time
import serial

# NG출력 폰트, 문자크기, 두께 설정.
font = cv2.FONT_HERSHEY_COMPLEX  # normal size sans-serif font
fontScale = 5
thickness = 4

# 화면의 해상도와 크기 불러오기.
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
screen_x, screen_y = screen_width/2, screen_height/2
int(screen_x), int(screen_y)

print(screen_x, screen_y)

def nothing(x):
    pass



cap = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars", cv2.WINDOW_NORMAL)

################## 데이터 입력 #########################
# Rivet coordinates 리벳의 위치 좌표 정보 입력 (x, y 좌표)
#[100, 100], [200, 200], [300, 300], [400, 400], [100, 400], [100, 300]
Rivets_coord = [[100, 100], [200, 200]]

Rivet_num = len(Rivets_coord)                     # 리벳의 갯수값 저장.

Rivet_tuple = []                                  # 튜플값을 저장할 리스트
for i in range(Rivet_num):
    Rivet_tuple.append(tuple(Rivets_coord[i]))    # 리벳 좌표값을 튜플로 변환후 리스트에 저장.

#테스트 출력
print(Rivet_tuple)
print(Rivets_coord[0][0], Rivets_coord[0][1])

# FIND_BLACK
cv2.createTrackbar("graybar", "Trackbars", 196, 255, nothing)
cv2.createTrackbar("bluebar", "Trackbars", 136,  255, nothing)
cv2.createTrackbar("greenbar", "Trackbars", 96, 255, nothing)
cv2.createTrackbar("redbar", "Trackbars", 76,  255, nothing)
cv2.createTrackbar("hsv hbar", "Trackbars", 136, 255, nothing)
cv2.createTrackbar("hsv sbar", "Trackbars", 145, 255, nothing)
cv2.createTrackbar("hsv vbar", "Trackbars", 78, 255, nothing)
cv2.createTrackbar("hsl hbar", "Trackbars", 127, 255, nothing)
cv2.createTrackbar("hsl sbar", "Trackbars", 107, 255, nothing) # 75
cv2.createTrackbar("hsl lbar", "Trackbars", 255, 255, nothing)

cv2.createTrackbar("bluebar_", "Trackbars", 18,  255, nothing) # 0
cv2.createTrackbar("greenbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("redbar_", "Trackbars", 0,  255, nothing)
cv2.createTrackbar("hsv hbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsv sbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsv vbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl hbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl sbar_", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("hsl lbar_", "Trackbars", 0, 255, nothing)

while True:
    _, frame = cap.read()

    # col,row,_ = frame.shape # 해상도
    # print(col,row)
    frame2 = frame.copy() # 영상원본

    frame = cv2.GaussianBlur(frame, (3, 3), 0)              # 원본에 가우시안 필터적용
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    blue, green, red = cv2.split(frame)                     # 원본에서 BGR 분리
    frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)      # HSV(색상, 채도, 명도) 분리
    h, s, v = cv2.split(frame_hsv)                          # 분리후 저장.

    frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)      # BGR -> HLS로
    H, L, S = cv2.split(frame_hls)                          # H,L,S 분리

    gray_c = cv2.getTrackbarPos("graybar", "Trackbars")         # 이하 필터값 튜닝 트렉바
    blue_c = cv2.getTrackbarPos("bluebar", "Trackbars")
    green_c = cv2.getTrackbarPos("greenbar", "Trackbars")
    red_c = cv2.getTrackbarPos("redbar", "Trackbars")
    hsv_h_c = cv2.getTrackbarPos("hsv hbar", "Trackbars")
    hsv_s_c = cv2.getTrackbarPos("hsv sbar", "Trackbars")
    hsv_v_c = cv2.getTrackbarPos("hsv vbar", "Trackbars")
    hsl_h_c = cv2.getTrackbarPos("hsl hbar", "Trackbars")
    hsl_s_c = cv2.getTrackbarPos("hsl sbar", "Trackbars")
    hsl_l_c = cv2.getTrackbarPos("hsl lbar", "Trackbars")

    gray_c_ = cv2.getTrackbarPos("graybar_", "Trackbars")
    blue_c_ = cv2.getTrackbarPos("bluebar_", "Trackbars")
    green_c_ = cv2.getTrackbarPos("greenbar_", "Trackbars")
    red_c_ = cv2.getTrackbarPos("redbar_", "Trackbars")
    hsv_h_c_ = cv2.getTrackbarPos("hsv hbar_", "Trackbars")
    hsv_s_c_ = cv2.getTrackbarPos("hsv sbar_", "Trackbars")
    hsv_v_c_ = cv2.getTrackbarPos("hsv vbar_", "Trackbars")
    hsl_h_c_ = cv2.getTrackbarPos("hsl hbar_", "Trackbars")
    hsl_s_c_ = cv2.getTrackbarPos("hsl sbar_", "Trackbars")
    hsl_l_c_ = cv2.getTrackbarPos("hsl lbar_", "Trackbars")

    _, gray1 = cv2.threshold(gray_frame,gray_c,255,cv2.THRESH_BINARY_INV)
    _, blue1 = cv2.threshold(blue, blue_c, 255, cv2.THRESH_BINARY_INV)
    _, green1 = cv2.threshold(green, green_c, 255, cv2.THRESH_BINARY_INV)
    _, red1   = cv2.threshold(red, red_c, 255, cv2.THRESH_BINARY_INV)
    _, h1 = cv2.threshold(h, hsv_h_c, 255, cv2.THRESH_BINARY_INV)
    _, s1 = cv2.threshold(s, hsv_s_c, 255, cv2.THRESH_BINARY_INV)
    _, v1 = cv2.threshold(v, hsv_v_c, 255, cv2.THRESH_BINARY_INV)
    _, H1 = cv2.threshold(H, hsl_h_c, 255, cv2.THRESH_BINARY_INV)
    _, L1 = cv2.threshold(L, hsl_s_c, 255, cv2.THRESH_BINARY_INV)
    _, S1 = cv2.threshold(S, hsl_l_c, 255, cv2.THRESH_BINARY_INV)

    _, gray_ = cv2.threshold(gray_frame, gray_c_, 255, cv2.THRESH_BINARY)
    _, blue_ = cv2.threshold(blue, blue_c_, 255, cv2.THRESH_BINARY)
    _, green_ = cv2.threshold(green, green_c_, 255, cv2.THRESH_BINARY)
    _, red_ = cv2.threshold(red, red_c_, 255, cv2.THRESH_BINARY)
    _, h_ = cv2.threshold(h, hsv_h_c_, 255, cv2.THRESH_BINARY)
    _, s_ = cv2.threshold(s, hsv_s_c_, 255, cv2.THRESH_BINARY)
    _, v_ = cv2.threshold(v, hsv_v_c_, 255, cv2.THRESH_BINARY)
    _, H_ = cv2.threshold(H, hsl_h_c_, 255, cv2.THRESH_BINARY)
    _, L_ = cv2.threshold(L, hsl_s_c_, 255, cv2.THRESH_BINARY)
    _, S_ = cv2.threshold(S, hsl_l_c_, 255, cv2.THRESH_BINARY)

    final_mask = gray1                                              # 하나씩 필터 mask를 씌움.
    final_mask = cv2.bitwise_and(final_mask, blue1)
    final_mask = cv2.bitwise_and(final_mask, green1)
    final_mask = cv2.bitwise_and(final_mask, red1)
    final_mask = cv2.bitwise_and(final_mask, h1)
    final_mask = cv2.bitwise_and(final_mask, s1)
    final_mask = cv2.bitwise_and(final_mask, v1)
    final_mask = cv2.bitwise_and(final_mask, H1)
    final_mask = cv2.bitwise_and(final_mask, L1)
    final_mask = cv2.bitwise_and(final_mask, S1)

    final_mask = cv2.bitwise_and(final_mask, gray_)
    final_mask = cv2.bitwise_and(final_mask, blue_)
    final_mask = cv2.bitwise_and(final_mask, green_)
    final_mask = cv2.bitwise_and(final_mask, red_)
    final_mask = cv2.bitwise_and(final_mask, v_)

    result = cv2.bitwise_and(frame2, frame2, mask=final_mask)       # 필터 결과 저장

    # _, contours, _ = cv2.findContours(final_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)  # 컨투어 찾기
    # if len(contours) != 0:
    #     for contour in contours:
    #         if (cv2.contourArea(contour) > 800) and (cv2.contourArea(contour) < 4500):  # **필요한 면적을 찾아 중심점 표시
    #             ball_area = cv2.contourArea(contour)
    #             mom = contour
    #             M = cv2.moments(mom)
    #             cx_origin = int(M['m10'] / M['m00'])
    #             cy_origin = int(M['m01'] / M['m00'])
    #             cv2.circle(frame, (cx_origin, cy_origin), 10, (0, 255, 0), -1) # Center of puck
    #             print(cx_origin, cy_origin) # 중심점값읽기

    reverse = cv2.bitwise_not(final_mask)
    reverse_copy = reverse.copy()

    # ** 리벳을 검출할 위치에 원으로 좌표 표시.
    for i in range(Rivet_num):
        reverse_copy = cv2.circle(reverse_copy, Rivet_tuple[i], 10, (0, 0, 255), -1)  # 가운데 점 픽셀값 확인용 (x,y)값으로 받음.
        frame = cv2.circle(frame, Rivet_tuple[i], 10, (0, 255, 0), -1)                # 원본에도 점 표시.

    # ** 한 픽셀당 Binary 값을 표시.
    # [y , x]의 픽셀값 입력받음.
    pixel_val_list = []
    for i in range(Rivet_num):
        pixel_val = reverse[Rivets_coord[i][1], Rivets_coord[i][0]]     # 픽셀값 저장 (0, 255)
        if pixel_val == 255:                                            # 검출된곳은 1, 검출되지 않을곳은 0으로 변환.
            pixel_val = 0
        else:
            pixel_val = 1

        pixel_val_list.append(pixel_val)                                # 변환된 값을 리스트에 추가
        pixel_sum = sum(pixel_val_list)                                 # 모든 픽셀의 합

    print(pixel_val_list, pixel_sum)                                    # 픽셀값과 합계 출력

    if pixel_sum == Rivet_num:
        # 리벳의 갯수와 픽셀의 값이 일치하면 합격
        pass
    else:
        # 그 외 불합격
        cv2.putText(frame, '**NG**', (50, 300), font, fontScale, (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow('Frame', frame)                      # 원본
    cv2.imshow('result', final_mask)                # 필터링후
    cv2.imshow('reverse', reverse)                  # 반전 (픽셀값을 찍어보기 위해 흑백)
    cv2.imshow('location_check', reverse_copy)      # 리벳위치 픽셀체크 위치 확인용

    if cv2.waitKey(1) & 0xff == ord('q'):
        break