From d8f92156c6e173b18529263157fd01d1c08bd997 Mon Sep 17 00:00:00 2001 From: PLYSHKA Date: Sat, 10 Sep 2022 21:46:37 +0400 Subject: [PATCH] Added commandline option for crop size on camera. Fixed cropping starting from wrong position. Changed thread count usage from half the threads to one. Added to comment with 100x multiplication neos notice. --- model_loader_no_Crop.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/model_loader_no_Crop.py b/model_loader_no_Crop.py index 5ffe062..10b8a89 100644 --- a/model_loader_no_Crop.py +++ b/model_loader_no_Crop.py @@ -4,12 +4,16 @@ import torch import time from pythonosc import udp_client import multiprocessing as mp +import sys OSCip="127.0.0.1" OSCport= 9000 #VR Chat OSC port client = udp_client.SimpleUDPClient(OSCip, OSCport) - +size_zoom = 300 +# get the size of zoomed image from first argument +if len(sys.argv) > 1: + size_zoom = int(sys.argv[1]) def vc(): vc.height = 1 @@ -22,26 +26,27 @@ if torch.cuda.is_available(): print("CUDA is available") else: device = torch.device("cpu") - thread = mp.cpu_count() - torch.set_num_threads(int(thread/2)) + torch.set_num_threads(1) print("CUDA is not available") #cheekPuff,cheekSquint_L,cheekSquint_R,noseSneer_L,noseSneer_R,jawOpen,jawForward,jawLeft,jawRight,mouthFunnel,mouthPucker,mouthLeft,mouthRight,mouthRollUpper,mouthRollLower,mouthShrugUpper,mouthShrugLower,mouthClose,mouthSmile_L,mouthSmile_R,mouthFrown_L,mouthFrown_R,mouthDimple_L,mouthDimple_R,mouthUpperUp_L,mouthUpperUp_R,mouthLowerDown_L,mouthLowerDown_R,mouthPress_L,mouthPress_R,mouthStretch_L,mouthStretch_R,tongueOut classes = ["cheekPuff", "cheekSquint_L", "cheekSquint_R", "noseSneer_L", "noseSneer_R", "jawOpen", "jawForward", "jawLeft", "jawRight", "mouthFunnel", "mouthPucker", "mouthLeft", "mouthRight", "mouthRollUpper", "mouthRollLower", "mouthShrugUpper", "mouthShrugLower", "mouthClose", "mouthSmile_L", "mouthSmile_R", "mouthFrown_L", "mouthFrown_R", "mouthDimple_L", "mouthDimple_R", "mouthUpperUp_L", "mouthUpperUp_R", "mouthLowerDown_L", "mouthLowerDown_R", "mouthPress_L", "mouthPress_R", "mouthStretch_L", "mouthStretch_R", "tongueOut"] model = mobilenetv2.mobilenetv2().to(device) model.load_state_dict(torch.load('BestTrainNeck1.pt',map_location=torch.device(device))) +model.eval() cap = cv2.VideoCapture(0) while True: #read the frame ret, frame = cap.read() #convert the frame to gr ayscale - centerx, centery = [int(frame.shape[0] / 2) - 50, int(frame.shape[1] / 2) - 50] - frame = frame[centerx:centerx + 100, centery:centery + 100] + centerx, centery = [int(frame.shape[0] / 2) - int(size_zoom / 2) , int(frame.shape[1] / 2) - int(size_zoom / 2)] + frame = frame[centerx:centerx + size_zoom, centery:centery + size_zoom] + frame = cv2.resize(frame,(100,100)) img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(img, (3, 3), 0) - image = gray.reshape(1,1, 100, 100) + image = gray.reshape(1,1, 100, 100) image = torch.from_numpy(image).to(device) image = image.float() start = time.time() @@ -53,7 +58,7 @@ while True: #get the avatar/parameter/jawOpen' output = output[0] output = output.tolist() - #multiply by 100 + #multiply by 100 for unity format, for neos - comment this line for 1x format output = [x * 100 for x in output] client.send_message("/cheekPuff", output[0]) client.send_message("/cheekSquintLeft", output[1]) @@ -89,7 +94,7 @@ while True: client.send_message("/mouthStretchRight", output[31]) client.send_message("/tongueOut", output[32]) #show the image - gray = cv2.resize(gray, (300,300), interpolation = cv2.INTER_AREA) + gray = cv2.resize(gray, (size_zoom, size_zoom), interpolation = cv2.INTER_AREA) cv2.imshow("image", gray) if cv2.waitKey(1) & 0xFF == ord('q'): break