diff --git a/multiperson/asdf.ipynb b/multiperson/asdf.ipynb new file mode 100644 index 0000000..9676ff7 --- /dev/null +++ b/multiperson/asdf.ipynb @@ -0,0 +1,178 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "initial_id", + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "SEQUENCE_1 = 'GCCAAGACAGTATCAGACAC' #Sequence 1 (Side Sequence)\n", + "SEQUENCE_2 = 'TGTCATGCCAAGACAGTATG' #Sequence 2 (Top Sequence)\n", + "MATRIX_ROW_N = len(SEQUENCE_1)+1 #Initiation Matrix Size (Rows)\n", + "MATRIX_COLUMN_N = len(SEQUENCE_2)+1 #Initiation Matrix Size (Columns)\n", + "MATCH_SCORE = 5 #Match Score\n", + "MISMATCH_SCORE = -3 #Mismatch Score\n", + "GAP_SCORE = -5 #Gap Points\n", + "GAP_CHARACTER = '-'#Character to Represent Gaps in Final Alignemnts\n", + "ALN_PATHWAYS = [] #Initiating List of Discovered aln Pathways\n", + "MATRIX = [[[[None] for i in range(2)] for i in range(MATRIX_COLUMN_N)] for i in range(MATRIX_ROW_N)] # Initiating Score Matrix\n", + "\n", + "def print_aln_details(aln): # Function to print steps of particular aln\n", + " print('\\n>>>>>>>>>>>>>>>> Aln #:'+str(aln[4])+' <<<<<<<<<<<<<<<<\\n\\n------------------Steps------------------\\n')\n", + " for elem in aln[3]:\n", + " print('Step: '+str(elem[0])+' -> Score = ' + str(elem[1]) + ' -> Dir: ' + ('\\u2190' if elem[2]==1 else ('\\u2196' if elem[2]==2 else ('\\u2191' if elem[2]==3 else 'Error'))))\n", + " print('\\n-------------Final aln-------------\\n')\n", + " print(aln[1]+'\\n'+aln[0])\n", + " return\n", + "\n", + "def print_all(ALIGNMENTS): #Function to print everything\n", + " print('Total Alignments: ' + str(len(ALIGNMENTS)))\n", + " print('Overall Score: '+str(ALIGNMENTS[0][3][0][1])+'\\n')\n", + " for elem in ALIGNMENTS:\n", + " print_aln_details(elem)\n", + " return\n", + "def print_alns_only(ALIGNMENTS): #Function to print only ALIGNMENTS\n", + " print('Total Alignments: ' + str(len(ALIGNMENTS)))\n", + " print('Overall Score: '+str(ALIGNMENTS[0][3][0][1])+'\\n')\n", + " for elem in ALIGNMENTS:\n", + " print(elem[0]+'\\n'+elem[1]+'\\n')\n", + " return\n", + "def find_each_path(c_i,c_j,path=''): #Nested function to discover new aln pathways\n", + " global ALN_PATHWAYS\n", + " i = c_i\n", + " j = c_j\n", + " if i == 0 and j==0:\n", + " ALN_PATHWAYS.append(path)\n", + " return 2\n", + " dir_t = len(MATRIX[i][j][1])\n", + " while dir_t<=1:\n", + " n_dir = MATRIX[i][j][1][0] if (i != 0 and j != 0) else (1 if i == 0 else (3 if j==0 else 0))\n", + " path = path + str(n_dir)\n", + " if n_dir == 1:\n", + " j=j-1\n", + " elif n_dir == 2:\n", + " i=i-1\n", + " j=j-1\n", + " elif n_dir == 3:\n", + " i=i-1\n", + " dir_t = len(MATRIX[i][j][1])\n", + " if i == 0 and j==0:\n", + " ALN_PATHWAYS.append(path)\n", + " return 3\n", + " if dir_t>1:\n", + " for dir_c in range(dir_t):\n", + " n_dir = MATRIX[i][j][1][dir_c] if (i != 0 and j != 0) else (1 if i == 0 else (3 if j==0 else 0))\n", + " tmp_path = path + str(n_dir)\n", + " if n_dir == 1:\n", + " n_i = i\n", + " n_j=j-1\n", + " elif n_dir == 2:\n", + " n_i=i-1\n", + " n_j=j-1\n", + " elif n_dir == 3:\n", + " n_i=i-1\n", + " n_j = j\n", + " find_each_path(n_i,n_j,tmp_path)\n", + " return len(ALN_PATHWAYS)\n", + "\n", + "#Main Code\n", + "# 배열 초기화\n", + "for i in range(MATRIX_ROW_N):\n", + " MATRIX[i][0] = [GAP_SCORE*i,[]]\n", + "for j in range(MATRIX_COLUMN_N):\n", + " MATRIX[0][j] = [GAP_SCORE*j,[]]\n", + "for i in range(1,MATRIX_ROW_N):\n", + " for j in range(1,MATRIX_COLUMN_N):\n", + " score = MATCH_SCORE if (SEQUENCE_1[i-1] == SEQUENCE_2[j-1]) else MISMATCH_SCORE # 일치하면 +8, 다르면 -5\n", + " h_val = MATRIX[i][j-1][0] + GAP_SCORE # 가로로 이동\n", + " d_val = MATRIX[i-1][j-1][0] + score # 대각선으로 이동\n", + " v_val = MATRIX[i-1][j][0] + GAP_SCORE # 세로로 이동\n", + " o_val = [h_val, d_val, v_val]\n", + " MATRIX[i][j] = [max(o_val), [i+1 for i,v in enumerate(o_val) if v==max(o_val)]] # 이들 중 최댓값 점수들과 이동 가능한 방향의 리스트 저장. 예시로 o_val = [10, 5, 10] 이면 MATRIX[i][j] = [10, [1, 3]]\n", + "\n", + "score = MATRIX[i][j][0] # 마지막 위치에서의 점수 = 76\n", + "\n", + "l_i = i\n", + "l_j = j\n", + "ALIGNMENTS = []\n", + "tot_aln = find_each_path(i,j) # 함수 실행\n", + "aln_count = 0\n", + "\n", + "for elem in ALN_PATHWAYS: # ['33332332222222222121112121', '33332332222222222211112121', '33332332222222222221111121', '33332332222222222222111111'] <- elems\n", + " i = l_i-1\n", + " j = l_j-1\n", + " side_aln = '' # 정렬된 서열\n", + " top_aln = '' # 기준 서열\n", + " step = 0\n", + " aln_info = []\n", + " for n_dir_c in range(len(elem)):\n", + " n_dir = elem[n_dir_c]\n", + " score = MATRIX[i+1][j+1][0]\n", + " step = step + 1\n", + " aln_info.append([step,score,n_dir]) # [[1, 76, '3'], [2, 76, '3'], [3, 76, '3'], [4, 76, '3'], [5, 76, '2']...\n", + " if n_dir == '2':\n", + " side_aln = side_aln + SEQUENCE_1[i]\n", + " top_aln = top_aln + SEQUENCE_2[j]\n", + " i=i-1\n", + " j=j-1\n", + " elif n_dir == '1':\n", + " side_aln = side_aln + GAP_CHARACTER\n", + " top_aln = top_aln + SEQUENCE_2[j]\n", + " j=j-1\n", + " elif n_dir == '3':\n", + " side_aln = side_aln + SEQUENCE_1[i]\n", + " top_aln = top_aln + GAP_CHARACTER\n", + " i=i-1\n", + " aln_count = aln_count + 1 # 정렬 결과 개수 증가\n", + " ALIGNMENTS.append([top_aln[::-1],side_aln[::-1],elem,aln_info,aln_count]) # 서열 역순 바꿔 저장\n", + "\n", + "print_alns_only(ALIGNMENTS)" + ], + "metadata": { + "collapsed": false, + "is_executing": true + }, + "id": "811f5a9699d77a02" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + }, + "id": "44cbc8cb1f0fe897" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/multiperson/main.py b/multiperson/main.py new file mode 100644 index 0000000..55b53d8 --- /dev/null +++ b/multiperson/main.py @@ -0,0 +1,91 @@ +import tensorflow as tf +import tensorflow_hub as hub +import cv2 +from matplotlib import pyplot as plt +import numpy as np +import pandas as pd + +EDGES = { + (0, 1): 'm', + (0, 2): 'c', + (1, 3): 'm', + (2, 4): 'c', + (0, 5): 'm', + (0, 6): 'c', + (5, 7): 'm', + (7, 9): 'm', + (6, 8): 'c', + (8, 10): 'c', + (5, 6): 'y', + (5, 11): 'm', + (6, 12): 'c', + (11, 12): 'y', + (11, 13): 'm', + (13, 15): 'm', + (12, 14): 'c', + (14, 16): 'c' +} + +def draw_connections(frame, keypoints, edges, confidence_threshold): + y, x, c = frame.shape + shaped = np.squeeze(np.multiply(keypoints, [y,x,1])) + + for edge, color in edges.items(): + p1, p2 = edge + y1, x1, c1 = shaped[p1] + y2, x2, c2 = shaped[p2] + if (c1 > confidence_threshold) & (c2 > confidence_threshold): + cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 4) + +def draw_keypoints(frame, keypoints, confidence_threshold): + y, x, c = frame.shape + shaped = np.squeeze(np.multiply(keypoints, [y, x, 1])) + posx = [] + posy = [] + for kp in shaped: + ky, kx, kp_conf = kp + if kp_conf > confidence_threshold: + posx.append(int(kx)) + posy.append(int(ky)) + # cv2.circle(frame, (int(kx), int(ky)), 6, (0, 255, 0), -1) + + # Draw bounding box if keypoints are detected + if len(posx) > 0 and len(posy) > 0: + max_x, max_y = max(posx), max(posy) + min_x, min_y = min(posx), min(posy) + + # Replace lines with cv2.rectangle + cv2.rectangle(frame, (min_x, min_y), (max_x, max_y), (0, 0, 255), 4) + # return (min_x, min_y), (max_x, max_y) + +# Function to loop through each person detected and render +def loop_through_people(frame, keypoints_with_scores, edges, confidence_threshold): + for person in keypoints_with_scores: + #draw_connections(frame, person, edges, confidence_threshold) + draw_keypoints(frame, person, confidence_threshold) + +model = hub.load('https://tfhub.dev/google/movenet/multipose/lightning/1') +movenet = model.signatures['serving_default'] + +cap = cv2.VideoCapture('novak.mp4') +while cap.isOpened(): + ret, frame = cap.read() + + # Resize image + img = frame.copy() + img = tf.image.resize_with_pad(tf.expand_dims(img, axis=0), 384,640) + input_img = tf.cast(img, dtype=tf.int32) + + # Detection section + results = movenet(input_img) + keypoints_with_scores = results['output_0'].numpy()[:,:,:51].reshape((6,17,3)) + + # Render keypoints + loop_through_people(frame, keypoints_with_scores, EDGES, 0.4) + + cv2.imshow('Movenet Multipose', frame) + + if cv2.waitKey(10) & 0xFF==ord('q'): + break +cap.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/region/hsv.named.py b/region/hsv.named.py new file mode 100644 index 0000000..41b8ae7 --- /dev/null +++ b/region/hsv.named.py @@ -0,0 +1,63 @@ +import cv2 +import numpy as np + +# 마우스 클릭 +def mouse_callback(event, x, y, flags, param): + global clicked_point, selected_area_name + if event == cv2.EVENT_LBUTTONDOWN: + clicked_point = (x, y) + selected_area_name = input("영역 이름 입력: ") + process_clicked_area() + +# 클릭한 좌표와 유사한 색상을 추출 & 연속된 픽셀을 묶는 함수 +def process_clicked_area(): + if clicked_point is not None: + hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) + clicked_hsv_color = hsv_image[clicked_point[1], clicked_point[0]] + + color_range = 15 # 색상 범위 + lower_bound = np.array([clicked_hsv_color[0] - color_range, 50, 50]) + upper_bound = np.array([clicked_hsv_color[0] + color_range, 255, 255]) + + mask = cv2.inRange(hsv_image, lower_bound, upper_bound) + result = cv2.bitwise_and(image, image, mask=mask) + + # 연속된 픽셀을 하나로 묶음 + gray_result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY) + _, binary = cv2.threshold(gray_result, 1, 255, cv2.THRESH_BINARY) + contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + + clicked_cluster = None # 클릭한 픽셀이 포함된 무리 찾기 + for contour in contours: + if cv2.pointPolygonTest(contour, clicked_point, False) >= 0: + clicked_cluster = contour + break + + if clicked_cluster is not None: # 영역 잘라내기 + x, y, w, h = cv2.boundingRect(clicked_cluster) + clicked_area = image[y:y + h, x:x + w] + + saved_areas[selected_area_name] = {'coordinates': (x, y, w, h)} # 추출한 영역을 임의로 정한 제목으로 저장 및 좌표 저장 + + cv2.imshow('Clicked Area - ' + selected_area_name, clicked_area) + +#초기 입력값 +clicked_point = None +selected_area_name = "" +image = cv2.imread('test1.jpg') +saved_areas = {} + +cv2.namedWindow('Image') +cv2.setMouseCallback('Image', mouse_callback) + +while True: + cv2.imshow('Image', image) + + key = cv2.waitKey(1) & 0xFF + if key == ord('a'): #창 닫기 + break +cv2.destroyAllWindows() + +# 저장된 영역과 좌표 출력 +for area_name, area_info in saved_areas.items(): + print(f"영역명: {area_name}, 좌표: {area_info['coordinates']}") \ No newline at end of file