Skip to content

Instantly share code, notes, and snippets.

@keidaroo
Created October 28, 2022 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keidaroo/74a175be52a14bc0aced4daed9a30680 to your computer and use it in GitHub Desktop.
Save keidaroo/74a175be52a14bc0aced4daed9a30680 to your computer and use it in GitHub Desktop.
# import the opencv library
import cv2
import numpy as np
import threading
from pythonosc import udp_client
from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc.osc_message_builder import OscMessageBuilder
IP = '127.0.0.1'
PORT = 25790
def sender(send_array):
address = "/ShadowPlay/contours"
# OSCメッセージの構築
client = udp_client.UDPClient(IP, PORT)
msg = OscMessageBuilder(address=address)
if len(send_array) != 12:
print("send_array is not 12: " + str(len(send_array)))
for i in send_array:
msg.add_arg(i)
m = msg.build()
# OSCメッセージの送信
client.send(m)
print("msg sent")
# define a video capture object
vid = cv2.VideoCapture(0)
while (True):
# Capture the video frame
# by frame
ret, frame = vid.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, frame = cv2.threshold(frame, 50, 255, cv2.THRESH_BINARY)
cv2.imshow('frame', frame)
contours, hierarchy = cv2.findContours(
frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contours, -1, (0, 255, 0), 10)
white_bg = np.zeros_like(frame)
cv2.drawContours(white_bg, contours, -1, 80, 10)
dots = []
for cnt in contours:
length = cv2.arcLength(cnt, True)
if (5 < length) and (length < 50):
dots.append(cnt)
# triangle_cnt.sort(key=lambda x: cv2.contourArea(x), reverse=True)
# if (len(triangle_cnt) == 0):
# print("no triangle")
# continue
cv2.drawContours(white_bg, dots, -1, 200, 10)
cv2.imshow('contours', white_bg)
send_cnt = 0
send_array = []
for cnt in dots:
send_array.append(cnt[0][0][0]/vid.get(cv2.CAP_PROP_FRAME_WIDTH)/10)
send_array.append(cnt[0][0][1]/vid.get(cv2.CAP_PROP_FRAME_WIDTH)/10)
send_cnt += 1
if (send_cnt == 6):
break
if (send_cnt < 6):
continue
if cv2.waitKey(1) & 0xFF == ord('s'):
print(send_array)
sender(send_array)
# Display the resulting frame
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment