-
-
Save electricbaka/c06286b7f2e458a4cc4b4a6e4ae60dd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#================================================== | |
# 使い方 | |
#================================================== | |
# Toolウィンドウ上部のトラックバーをマウスでドラッグし拡大・縮小 | |
# Toolウィンドウ内のアイテム画像をマウスでドラッグし移動 | |
# Eye Pointウィンドウの表示数値がEyePointの座標となる | |
#================================================== | |
# import | |
#================================================== | |
import cv2 | |
import numpy as np | |
from pngoverlay import PNGOverlay | |
#================================================== | |
# 設定 | |
#================================================== | |
# 背景画像 | |
image_background = 'image/photo.jpg' | |
# アイテム画像 | |
image_item = 'image/6629_trim_small.png' | |
# トラックバー最大値(アイテム画像の拡大縮小の分解能) | |
track_position_max = 1000 | |
# トラックバー最大時のアイテム画像の倍率 | |
scale_rate = 2 | |
# ディープラーニング推論した目の位置の座標(背景画像を変更したら要修正) | |
eye_left_x, eye_left_y, eye_right_x, eye_right_y = 366, 162, 432, 162 # y座標は同じ位置になるように微修正済 | |
#================================================== | |
# マウス、トラックバーのコールバック | |
#================================================== | |
# マウスのコールバック関数 | |
def callback_mouse(event, x, y, flags, param): | |
global item_x, item_y, flag_mouse_drag, cursor_offset_x, cursor_offset_y | |
if event == cv2.EVENT_LBUTTONDOWN: | |
if x >= item_x - item.width/2 and x <= item_x + item.width/2 and y >= item_y - item.height/2 and y <= item_y + item.height/2: | |
flag_mouse_drag = True | |
cursor_offset_x = item_x - x | |
cursor_offset_y = item_y - y | |
elif event == cv2.EVENT_MOUSEMOVE: | |
if flag_mouse_drag == True: | |
item_x = x + cursor_offset_x | |
item_y = y + cursor_offset_y | |
elif event == cv2.EVENT_LBUTTONUP: | |
flag_mouse_drag = False | |
# トラックバーのコールバック関数 | |
def changeTrack(val): | |
global track_position | |
track_position = cv2.getTrackbarPos('scale', 'Tool') | |
#0はエラーになるので強制的に1にする | |
if track_position <= 0: | |
track_position = 1 | |
# マウスコールバック関数の登録 | |
cv2.namedWindow('Tool') | |
cv2.setMouseCallback('Tool', callback_mouse) | |
# トラックバーの生成とコールバック登録 | |
track_position = int(track_position_max/2) # トラックバー初期位置 | |
cv2.createTrackbar('scale', 'Tool', track_position, track_position_max, changeTrack) | |
#================================================== | |
# 準備 | |
#================================================== | |
# 透過PNG画像のインスタンス生成 | |
item = PNGOverlay(image_item) | |
# EyePoint確認用の別ウィンドウにも透過PNG画像インスタンス生成 | |
item2 = PNGOverlay(image_item) | |
# マウスカーソルとアイテム画像の中心画像のオフセット | |
cursor_offset_x = 0 | |
cursor_offset_y = 0 | |
# マウスドラッグ中を示すフラグ | |
flag_mouse_drag = False | |
# アイテム画像の中心座標 | |
scale = track_position/track_position_max * scale_rate | |
item.resize(scale) | |
item_x = int(item.width/2) | |
item_y = int(item.height/2) | |
#================================================== | |
# メインループ | |
#================================================== | |
while True: | |
key = cv2.waitKey(1) | |
if key != -1: | |
break | |
# 背景画像読み込み | |
frame = cv2.imread(image_background) | |
# 透過PNGを描画 | |
scale = track_position/track_position_max * scale_rate | |
item.resize(scale) | |
item.show(frame, item_x , item_y) | |
cv2.imshow('Tool', frame) | |
# EyePointの計算 | |
item_origin_x = item_x - int(item.width/2) #frameに対するアイテム画像の原点座標x | |
item_origin_y = item_y - int(item.height/2) #frameに対するアイテム画像の原点座標y | |
EyePoint_left_x = int((eye_left_x - item_origin_x) / scale) | |
EyePoint_left_y = int((eye_left_y - item_origin_y) / scale) | |
EyePoint_right_x = int((eye_right_x - item_origin_x) / scale) | |
EyePoint_right_y = int((eye_right_y - item_origin_y) / scale) | |
# EyePointの表示文字列 | |
text_left = 'L : ' + str(EyePoint_left_x) + ', ' + str(EyePoint_left_y) | |
text_right = 'R : ' + str(EyePoint_right_x) + ', ' + str(EyePoint_right_y) | |
# EyePoint確認用の別ウィンドウ | |
frame2 = np.zeros((item2.height + 300, item2.width, 3), np.uint8) + 255 # 白画生成 | |
item2.show(frame2, int(item2.width/2) , int(item2.height/2)) | |
cv2.circle(frame2, (EyePoint_left_x, EyePoint_left_y), 10, (0, 0, 255), thickness=-1) | |
cv2.circle(frame2, (EyePoint_right_x, EyePoint_right_y), 10, (0, 0, 255), thickness=-1) | |
cv2.putText(frame2, text_left, (20, frame2.shape[0] - 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) | |
cv2.putText(frame2, text_right, (20, frame2.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) | |
cv2.imshow('Eye Point', frame2) | |
#================================================== | |
# 終了処理 | |
#================================================== | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment