fix: resolved Python demos error on Windows #58

This commit is contained in:
esimov
2022-11-09 11:30:24 +02:00
parent efdb94e6e2
commit ce57eb8022
3 changed files with 17 additions and 29 deletions

View File

@@ -1,6 +1,5 @@
from ctypes import *
import subprocess
import numpy as np
import os
import cv2
@@ -8,7 +7,6 @@ import time
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
pigo = cdll.LoadLibrary('./pigo.so')
os.system('rm pigo.so')
MAX_NDETS = 2048
@@ -21,7 +19,7 @@ class GoPixelSlice(Structure):
# Obtain the camera pixels and transfer them to Go through Ctypes.
def process_frame(pixs):
dets = np.zeros(3 * MAX_NDETS, dtype=np.float32)
dets = np.ones(3 * MAX_NDETS, dtype=np.float32)
pixels = cast((c_ubyte * len(pixs))(*pixs), POINTER(c_ubyte))
# call FindFaces
@@ -33,14 +31,13 @@ def process_frame(pixs):
ndets = pigo.FindFaces(faces)
data_pointer = cast(ndets, POINTER((c_longlong * 3) * MAX_NDETS))
if data_pointer :
buffarr = ((c_longlong * 3) * MAX_NDETS).from_address(addressof(data_pointer.contents))
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 3,))
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(3,3))
# The first value of the buffer aray represents the buffer length.
dets_len = res[0][0]
res = np.delete(res, 0, 0) # delete the first element from the array
dets = list(res.reshape(-1, 3))[0:dets_len]
dets = np.reshape(res, (-1, 3))[0:dets_len]
return dets
@@ -49,10 +46,6 @@ cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Changing the camera resolution introduce a short delay in the camera initialization.
# For this reason we should delay the object detection process with a few milliseconds.
time.sleep(0.4)
while(True):
ret, frame = cap.read()
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
@@ -65,7 +58,7 @@ while(True):
for det in dets:
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2)
cv2.imshow('', frame)
cv2.imshow('Face detection', frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break

View File

@@ -8,7 +8,6 @@ import time
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
pigo = cdll.LoadLibrary('./pigo.so')
os.system('rm pigo.so')
MAX_NDETS = 2048
@@ -35,7 +34,7 @@ def process_frame(pixs):
if data_pointer :
buffarr = ((c_longlong * 3) * MAX_NDETS).from_address(addressof(data_pointer.contents))
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 3,))
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(3,3))
# The first value of the buffer aray represents the buffer length.
dets_len = res[0][0]
@@ -45,15 +44,12 @@ def process_frame(pixs):
return dets
width, height = 640, 480
# initialize the camera
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Changing the camera resolution introduce a short delay in the camera initialization.
# For this reason we should delay the object detection process with a few milliseconds.
time.sleep(0.4)
while(True):
ret, frame = cap.read()
pixs = np.ascontiguousarray(frame[:, :, 1]).flatten()
@@ -64,10 +60,10 @@ while(True):
if dets is not None:
for det in dets:
mask = np.zeros((height, width, 3), dtype=np.uint8)
mask = cv2.circle(mask, (int(det[1]), int(det[0])), int(det[2]/1.8), np.array([255, 255, 255]), -1)
mask = cv2.circle(mask, (int(det[1]), int(det[0])), int(det[2]/2.0), (255, 255, 255), -1)
frame = np.where(mask!=np.array([255, 255, 255]), frame, cv2.blur(frame, (30, 30), 0))
cv2.imshow('', frame)
cv2.imshow('Faceblur demo', frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break

View File

@@ -68,8 +68,7 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" {
#endif
extern GoUintptr FindFaces(GoSlice p0);
extern __declspec(dllexport) GoUintptr FindFaces(GoSlice pixels);
#ifdef __cplusplus
}