#!/usr/bin/env python
#改进后加入摄像头画面,实现声像融合,校正声像仪画面,配合usb4.cfg使用,加入热力图(一个声源座标点),标准圆(heatmappy)那个方案尝试多个点绘图尝试一下,可以显示,红蓝正常显示
import math
import socket
import sys
import threading
import random
import os
import time
import struct
import cv2
import signal
import json
import ast
import numpy as np
from heatmappy import Heatmapper
from PIL import Image
import matplotlib.pyplot as plt
matrix = np.array([[1150.4796065932892, 0.0, 866.7906072916296],
[0.0, 1154.5867399421018, 498.44391507650516],
[0.0, 0.0, 1.0]], dtype=float)
new_camera_matrix = np.array([[561.1795654296875, 0.0, 854.4371857272781],
[0.0, 523.6474609375, 371.79368114244426],
[0.0, 0.0, 1.0]], dtype=float)
dist = np.array([[-0.4210920702510668, 0.2214943232573482, 0.0012200103644413377, 0.001434741235348292, -0.06668565568366154]], dtype=float)
cmd ='build/bin/odaslive -c config/usb4.cfg'
os.system(cmd)
stop = False
HOST = "0.0.0.0"
PORT = 9000
SOCK_ADDR = (HOST, PORT)
def generate_random_points(center, x_range, y_range, num_points):
points = []
for _ in range(num_points):
x = random.randint(center[0] - x_range, center[0] + x_range)
y = random.randint(center[1] - y_range, center[1] + y_range)
points.append((x, y)) # 使用元组存储坐标点
return points
class SocketClientObject(object):
def __init__(self, socket, address ):
self.socket = socket
self.address = address
class ClientThread(threading.Thread):
def __init__(self, client_object):
threading.Thread.__init__(self)
self.client_object = client_object
def run(self):
global running
while running:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
count = 0
while True:
ret, frame = cap.read()
frame = cv2.undistort(frame, matrix, dist, new_camera_matrix)
frame = cv2.resize(frame, (1920, 1080))
data = self.client_object.socket.recv(20480)
data = data.decode("utf-8")
data = data.replace("\n", "")
try:
src = (data.split('[')[1]).split(']')[0]
items = src.split(", ")
target = json.loads(items[0])
x = float(target["x"])
y = -float(target["y"])
z = float(target["z"])
camera_focal_length = 806
az = math.atan2(y, x)
inc = math.acos(z)
print(f'方位角={az},俯仰角={math.degrees(inc)}')
s = int(camera_focal_length * np.tan(inc))
m = int(s * np.cos(az))
n = int(s * np.sin(az))
center_x = 960 + m
center_y = 540 - n
center = (center_x, center_y) # 中心点坐标
example_points = [] # 将中心点添加到 example_points 中
random_points = generate_random_points(center, 40, 40, 5)
example_points.extend(random_points) # 将随机生成的坐标点添加到 example_points 中
print(f'参考点={example_points}')
frame_pil = Image.fromarray(frame)
heatmapper = Heatmapper(point_strength=1, opacity=0.9, point_diameter=300)
heatmap = heatmapper.heatmap_on_img(example_points, frame_pil)
print("热度图生成完成")
#heatmap.save(f'heatmap_{count}.png') # 保存带有不同文件名的热度图
#count += 1 # 更新计数
heatmap_np = np.array(heatmap)
heatmap_np = cv2.cvtColor(heatmap_np, cv2.COLOR_BGR2RGB)
cv2.imshow('Heatmap', heatmap_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
print("发生异常:", e)
cap.release()
cv2.destroyAllWindows()
self.client_object.socket.close()
def stop_handler(signum, frame):
global running
running = False
signal.signal(signal.SIGINT, stop_handler)
def main():
global running
running = True
try:
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock1.bind(SOCK_ADDR)
sock1.listen(5)
while running:
(clientsocket, address) = sock1.accept()
print (" Accept client: ", address)
ct = ClientThread(SocketClientObject(clientsocket, address))
ct.start()
except:
print ("#! EXC: ", sys.exc_info())
sock1.close()
print ("THE END! Goodbye!")
if __name__ == "__main__":
main()
#!/usr/bin/env python
#改进后加入摄像头画面,实现声像融合,校正声像仪画面,配合usb4.cfg使用,加入热力图(一个声源座标点)
import math
import socket
import sys
import threading
import random
import os
import time
import struct
import cv2
import signal
import json
import ast
import numpy as np
from heatmappy import Heatmapper
from PIL import Image
matrix = np.array([[1150.4796065932892, 0.0, 866.7906072916296],
[0.0, 1154.5867399421018, 498.44391507650516],
[0.0, 0.0, 1.0]], dtype=float)
new_camera_matrix = np.array([[561.1795654296875, 0.0, 854.4371857272781],
[0.0, 523.6474609375, 371.79368114244426],
[0.0, 0.0, 1.0]], dtype=float)
dist = np.array([[-0.4210920702510668, 0.2214943232573482, 0.0012200103644413377, 0.001434741235348292, -0.06668565568366154]], dtype=float)
cmd ='build/bin/odaslive -c config/usb4.cfg'
os.system(cmd)
stop = False
HOST = "0.0.0.0"
PORT = 9000
SOCK_ADDR = (HOST, PORT)
class SocketClientObject(object):
def __init__(self, socket, address ):
self.socket = socket
self.address = address
class ClientThread(threading.Thread):
def __init__(self, client_object):
threading.Thread.__init__(self)
self.client_object = client_object
def run(self):
global running
while running == True:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
while True:
ret, frame = cap.read()
frame = cv2.undistort(frame, matrix, dist, new_camera_matrix)
frame = cv2.resize(frame, (1920, 1080))
# cv2.imshow('fff', frame)
# img = np.zeros((800,800,3),np.uint8)
data = self.client_object.socket.recv(20480)
data = data.decode("utf-8")
data = data.replace("\n", "")
try:
src = (data.split('[')[1]).split(']')[0]
items = src.split(", ")
target = json.loads(items[0])
# print(f'data={data}')
# print(f'target={target}')
# energy = int(float(target["E"]) * 255)
#if (energy > 80):
x = float(target["x"])
y = -float(target["y"])
z = float(target["z"])
# print(f'x={x},y={y},z={z}')
camera_focal_length = 806 # 以像素为单位计算的虚拟焦距
az = math.atan2(y, x) # 方位角 以x轴正方向为起始边逆时针旋转的角度
inc = math.acos(z) # 俯仰角 与z轴的夹角
# print(f'{math.sqrt(x * x + y * y + z*z)}')
print(f'方位角={az},俯仰角={math.degrees(inc)}')
s = int(camera_focal_length * np.tan(inc)) # 计算目标映射位置偏离中心距离 单位为像素
m = int(s * np.cos(az)) # 目标映射位置在x轴上偏离中心的距离
n = int(s * np.sin(az)) # 目标映射位置在y轴上偏离中心的距离
# print(f'x轴上偏离中心的距离={m},y轴上偏离中心的距离={n}')
center_x = 960 + m # 目标映射位置的x轴坐标
center_y = 540 - n # 目标映射位置的y轴坐标
example_points = [(center_x, center_y)]
print(f'参考点={example_points}')
# 检查类型和数据类型
print(type(frame))
frame_pil = Image.fromarray(frame)
heatmapper = Heatmapper(point_diameter=300,point_strength=1)
heatmap = heatmapper.heatmap_on_img(example_points, frame_pil)
print("热度图生成完成")
# 将热度图转换为 NumPy 数组
heatmap_np = np.array(heatmap)
cv2.imshow('Heatmap', heatmap_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print ("problem1")
cap.release()
cv2.destroyAllWindows()
cv2.destroyAllWindows()
self.client_object.socket.close()
def stop_handler(signum, frame):
global running
running = False
signal.signal(signal.SIGINT, stop_handler)
def main():
global running
running = True
try:
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock1.bind(SOCK_ADDR)
sock1.listen(5)
while running:
(clientsocket, address) = sock1.accept()
print (" Accept client: ", address)
ct = ClientThread(SocketClientObject(clientsocket, address))
ct.start()
except:
print ("#! EXC: ", sys.exc_info())
sock1.close()
print ("THE END! Goodbye!")
if __name__ == "__main__":
main()
#!/usr/bin/env python
#改进后加入摄像头画面,实现声像融合,校正声像仪画面,配合usb4.cfg使用,加入热力图(一个时间戳的四个声源座标点)正常显示
import math
import socket
import sys
import threading
import random
import os
import time
import struct
import cv2
import signal
import json
import ast
import numpy as np
from heatmappy import Heatmapper
from PIL import Image
matrix = np.array([[1150.4796065932892, 0.0, 866.7906072916296],
[0.0, 1154.5867399421018, 498.44391507650516],
[0.0, 0.0, 1.0]], dtype=float)
new_camera_matrix = np.array([[561.1795654296875, 0.0, 854.4371857272781],
[0.0, 523.6474609375, 371.79368114244426],
[0.0, 0.0, 1.0]], dtype=float)
dist = np.array([[-0.4210920702510668, 0.2214943232573482, 0.0012200103644413377, 0.001434741235348292, -0.06668565568366154]], dtype=float)
cmd ='build/bin/odaslive -c config/usb4.cfg'
os.system(cmd)
stop = False
HOST = "0.0.0.0"
PORT = 9000
SOCK_ADDR = (HOST, PORT)
class SocketClientObject(object):
def __init__(self, socket, address ):
self.socket = socket
self.address = address
class ClientThread(threading.Thread):
def __init__(self, client_object):
threading.Thread.__init__(self)
self.client_object = client_object
def run(self):
global running
while running == True:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
while True:
ret, frame = cap.read()
frame = cv2.undistort(frame, matrix, dist, new_camera_matrix)
frame = cv2.resize(frame, (1920, 1080))
# cv2.imshow('fff', frame)
# img = np.zeros((800,800,3),np.uint8)
data = self.client_object.socket.recv(20480)
data = data.decode("utf-8")
data = data.replace("\n", "")
#print(f'data={data}')
try:
src = (data.split('[')[1]).split(']')[0]
items = src.split(", ")
print(f'data={data}')
print(f'src={src}')
print(f'items={items}')
all_example_points = []
for item in items:
# 解析JSON数据
target = json.loads(item)
# 计算坐标
x = float(target["x"])
y = -float(target["y"])
z = float(target["z"])
camera_focal_length = 806
az = math.atan2(y, x)
inc = math.acos(z)
s = int(camera_focal_length * np.tan(inc))
m = int(s * np.cos(az))
n = int(s * np.sin(az))
center_x = 960 + m
center_y = 540 - n
example_points = (center_x, center_y)
# 将计算结果添加到列表中
all_example_points.append(example_points)
print(f'参考点={all_example_points}')
frame_pil = Image.fromarray(frame)
heatmapper = Heatmapper(point_diameter=500,point_strength=1)
heatmap = heatmapper.heatmap_on_img(all_example_points, frame_pil)
print("热度图生成完成")
# 将热度图转换为 NumPy 数组
heatmap_np = np.array(heatmap)
cv2.imshow('Heatmap', heatmap_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print ("problem1")
cap.release()
cv2.destroyAllWindows()
cv2.destroyAllWindows()
self.client_object.socket.close()
def stop_handler(signum, frame):
global running
running = False
signal.signal(signal.SIGINT, stop_handler)
def main():
global running
running = True
try:
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock1.bind(SOCK_ADDR)
sock1.listen(5)
while running:
(clientsocket, address) = sock1.accept()
print (" Accept client: ", address)
ct = ClientThread(SocketClientObject(clientsocket, address))
ct.start()
except:
print ("#! EXC: ", sys.exc_info())
sock1.close()
print ("THE END! Goodbye!")
if __name__ == "__main__":
main()
#!/usr/bin/env python
#改进后加入摄像头画面,实现声像融合,校正声像仪画面,配合usb4.cfg使用,加入热力图(一个声源座标点),标准圆那个方案尝试多个点绘图尝试一下(实际声源的四个点),可以显示,红蓝正常显示
import math
import socket
import sys
import threading
import random
import os
import time
import struct
import cv2
import signal
import json
import ast
import numpy as np
from heatmappy import Heatmapper
from PIL import Image
import matplotlib.pyplot as plt
matrix = np.array([[1150.4796065932892, 0.0, 866.7906072916296],
[0.0, 1154.5867399421018, 498.44391507650516],
[0.0, 0.0, 1.0]], dtype=float)
new_camera_matrix = np.array([[561.1795654296875, 0.0, 854.4371857272781],
[0.0, 523.6474609375, 371.79368114244426],
[0.0, 0.0, 1.0]], dtype=float)
dist = np.array([[-0.4210920702510668, 0.2214943232573482, 0.0012200103644413377, 0.001434741235348292, -0.06668565568366154]], dtype=float)
cmd ='build/bin/odaslive -c config/usb4.cfg'
os.system(cmd)
stop = False
HOST = "0.0.0.0"
PORT = 9000
SOCK_ADDR = (HOST, PORT)
def generate_random_points(center, x_range, y_range, num_points):
points = []
for _ in range(num_points):
x = random.randint(center[0] - x_range, center[0] + x_range)
y = random.randint(center[1] - y_range, center[1] + y_range)
points.append((x, y)) # 使用元组存储坐标点
return points
class SocketClientObject(object):
def __init__(self, socket, address ):
self.socket = socket
self.address = address
class ClientThread(threading.Thread):
def __init__(self, client_object):
threading.Thread.__init__(self)
self.client_object = client_object
def run(self):
global running
while running:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
count = 0
while True:
ret, frame = cap.read()
frame = cv2.undistort(frame, matrix, dist, new_camera_matrix)
frame = cv2.resize(frame, (1920, 1080))
data = self.client_object.socket.recv(20480)
data = data.decode("utf-8")
data = data.replace("\n", "")
try:
src = (data.split('[')[1]).split(']')[0]
items = src.split(", ")
all_example_points = []
for item in items:
# 解析JSON数据
target = json.loads(item)
# 计算坐标
x = float(target["x"])
y = -float(target["y"])
z = float(target["z"])
camera_focal_length = 806
az = math.atan2(y, x)
inc = math.acos(z)
s = int(camera_focal_length * np.tan(inc))
m = int(s * np.cos(az))
n = int(s * np.sin(az))
center_x = 960 + m
center_y = 540 - n
example_points = (center_x, center_y)
# 将计算结果添加到列表中
all_example_points.append(example_points)
frame_pil = Image.fromarray(frame)
heatmapper = Heatmapper(point_strength=1, opacity=0.9, point_diameter=500)
heatmap = heatmapper.heatmap_on_img(all_example_points, frame_pil)
print("热度图生成完成")
#heatmap.save(f'heatmap_{count}.png') # 保存带有不同文件名的热度图
#count += 1 # 更新计数
heatmap_np = np.array(heatmap)
heatmap_np = cv2.cvtColor(heatmap_np, cv2.COLOR_BGR2RGB)
cv2.imshow('Heatmap', heatmap_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
print("发生异常:", e)
cap.release()
cv2.destroyAllWindows()
self.client_object.socket.close()
def stop_handler(signum, frame):
global running
running = False
signal.signal(signal.SIGINT, stop_handler)
def main():
global running
running = True
try:
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock1.bind(SOCK_ADDR)
sock1.listen(5)
while running:
(clientsocket, address) = sock1.accept()
print (" Accept client: ", address)
ct = ClientThread(SocketClientObject(clientsocket, address))
ct.start()
except:
print ("#! EXC: ", sys.exc_info())
sock1.close()
print ("THE END! Goodbye!")
if __name__ == "__main__":
main()