Commit 77f0dee9 authored by Qihao Ye's avatar Qihao Ye
Browse files

codes update

parent e75068f6
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+2 −0
Original line number Diff line number Diff line

.DS_Store

Code/car_target.py

0 → 100644
+475 −0

File added.

Preview size limit exceeded, changes collapsed.

Code/circle_test.py

0 → 100644
+74 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__date__ = '2018/01/22'


import cv2
import cv2.cv as cv
import numpy as np
import tool
import serial
#import binascill
import string

'''
This is a 'hello world' test for pantilt test.
Print circle_test.pdf and put the paper close to camera.
'''

def sent(t,x,y):
    if x>320:
        yaw_angle = int(3050 - ((x-320)/320 * 3050))
    else:
        yaw_angle = int(3050 + ((320-x)/320 * 2650))
    if y<240:
        pitch_angle = int(5000 - ((240-y)/240 * 5000))
    else:
        pitch_angle = int(5000 + ((y-240)/240 * 1000))
    #t.write(bytes.fromhex(yaw_angle))
    #t.write(bytes.fromhex(pitch_angle))
    mes = bytearray([0,0,0,0,0,0,0,0,0])
    mes[0] = 0xFA
    mes[1] = 0x04
    mes[2] = 0x00
    mes[3] = yaw_angle & 0xFF
    mes[4] = (yaw_angle>>8) & 0xFF
    mes[5] = pitch_angle & 0xFF
    mes[6] = (pitch_angle>>8) & 0xFF
    mes[7] = 0x00
    mes[8] = 0xFB
    print([yaw_angle,pitch_angle])
    t.write(mes)

def func_detect_circle(frame,t):
    result = [0,0,0]
#    frame = cv2.resize(frame, (480, 360))
    b,g,r = cv2.split(frame)
    sp = frame.shape
    #480 640
    r = cv2.pyrDown(r)
    circles1 = cv2.HoughCircles(r,cv.CV_HOUGH_GRADIENT,1.2,1200,param1=250,param2=80,minRadius=30,maxRadius=0)
    if np.any(circles1):
        circles = circles1[0,:,:]
        circles = np.uint16(np.around(circles))
        for i in circles[:]:
            cv2.circle(frame, (2*i[0], 2*i[1]), 2*i[2], (0, 0, 255), 5)
            #i[0] np.uint16
            x = 0.0+2*i[0]
            y = 0.0+2*i[1]
            sent(t,x,y)
            if i[2] > result[2]:
                result = list(i)
    cv2.imshow('frame', frame)
    cv2.waitKey(1)
    #return result if sum(result) else []
    return [2*i[0], 2*i[1]] if sum(result) else []

t = serial.Serial('/dev/ttyUSB0',115200)
tool.func_tool_set_quit()
cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    func_detect_circle(frame,t)

    #print (func_detect_circle(frame,t))
 No newline at end of file

Code/tool.py

0 → 100644
+56 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__date__ = '2018/01/22'


import signal
import sys
import cv2


def func_tool_quit(signum, frame):
    print("TOOL : interupt " + str(signum) + " , program terminate")
    sys.exit()


def func_tool_set_quit():
    '''
    Set Ctrl-C signal to quit.
    '''
    signal.signal(signal.SIGINT, func_tool_quit)


def func_tool_folder(file_dir, file_type):
    '''
    Return a list of a type of files in a folder.
    
    Args:
        file_dir: A folder to find file list.
        file_type: A type of file.
    
    Rerturns:
        A list of string with absolute path.
    '''
    import os
    file_list = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == "." + file_type:
                file_list.append(os.path.join(root, file))
    return file_list


def func_tool_mouth_callback_show_pix(event, x, y, a, b):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("x, y, b, g, r " + str(x) + " " + str(y) + " " + str(b[y, x]))


def func_tool_set_mouth_callback_show_pix(window, mat):
    '''
    Set mouth callback for a window and a mat. Left click a pixel and consol will print [x, y, b, g, r] 
    
    Args:
        window: A window to set mat callback
        mat: A mat to print pixel
    '''
    cv2.setMouseCallback(window, func_tool_mouth_callback_show_pix, mat)