mirror of
https://github.com/esimov/pigo.git
synced 2025-10-16 05:00:35 +08:00
fix: resolved Python demos error on Windows #58
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
from ctypes import *
|
from ctypes import *
|
||||||
|
|
||||||
import subprocess
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import os
|
import os
|
||||||
import cv2
|
import cv2
|
||||||
@@ -8,7 +7,6 @@ import time
|
|||||||
|
|
||||||
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
|
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
|
||||||
pigo = cdll.LoadLibrary('./pigo.so')
|
pigo = cdll.LoadLibrary('./pigo.so')
|
||||||
os.system('rm pigo.so')
|
|
||||||
|
|
||||||
MAX_NDETS = 2048
|
MAX_NDETS = 2048
|
||||||
|
|
||||||
@@ -21,7 +19,7 @@ class GoPixelSlice(Structure):
|
|||||||
|
|
||||||
# Obtain the camera pixels and transfer them to Go through Ctypes.
|
# Obtain the camera pixels and transfer them to Go through Ctypes.
|
||||||
def process_frame(pixs):
|
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))
|
pixels = cast((c_ubyte * len(pixs))(*pixs), POINTER(c_ubyte))
|
||||||
|
|
||||||
# call FindFaces
|
# call FindFaces
|
||||||
@@ -33,14 +31,13 @@ def process_frame(pixs):
|
|||||||
ndets = pigo.FindFaces(faces)
|
ndets = pigo.FindFaces(faces)
|
||||||
data_pointer = cast(ndets, POINTER((c_longlong * 3) * MAX_NDETS))
|
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))
|
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.
|
# The first value of the buffer aray represents the buffer length.
|
||||||
dets_len = res[0][0]
|
dets_len = res[0][0]
|
||||||
res = np.delete(res, 0, 0) # delete the first element from the array
|
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
|
return dets
|
||||||
|
|
||||||
@@ -49,10 +46,6 @@ cap = cv2.VideoCapture(0)
|
|||||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
||||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
|
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):
|
while(True):
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
|
pixs = np.ascontiguousarray(frame[:, :, 1].reshape((frame.shape[0], frame.shape[1])))
|
||||||
@@ -65,7 +58,7 @@ while(True):
|
|||||||
for det in dets:
|
for det in dets:
|
||||||
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2)
|
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'):
|
if cv2.waitKey(5) & 0xFF == ord('q'):
|
||||||
break
|
break
|
||||||
|
@@ -8,7 +8,6 @@ import time
|
|||||||
|
|
||||||
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
|
os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
|
||||||
pigo = cdll.LoadLibrary('./pigo.so')
|
pigo = cdll.LoadLibrary('./pigo.so')
|
||||||
os.system('rm pigo.so')
|
|
||||||
|
|
||||||
MAX_NDETS = 2048
|
MAX_NDETS = 2048
|
||||||
|
|
||||||
@@ -35,7 +34,7 @@ def process_frame(pixs):
|
|||||||
|
|
||||||
if data_pointer :
|
if data_pointer :
|
||||||
buffarr = ((c_longlong * 3) * MAX_NDETS).from_address(addressof(data_pointer.contents))
|
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.
|
# The first value of the buffer aray represents the buffer length.
|
||||||
dets_len = res[0][0]
|
dets_len = res[0][0]
|
||||||
@@ -45,15 +44,12 @@ def process_frame(pixs):
|
|||||||
return dets
|
return dets
|
||||||
|
|
||||||
width, height = 640, 480
|
width, height = 640, 480
|
||||||
|
|
||||||
# initialize the camera
|
# initialize the camera
|
||||||
cap = cv2.VideoCapture(0)
|
cap = cv2.VideoCapture(0)
|
||||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
|
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
|
||||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
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):
|
while(True):
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
pixs = np.ascontiguousarray(frame[:, :, 1]).flatten()
|
pixs = np.ascontiguousarray(frame[:, :, 1]).flatten()
|
||||||
@@ -64,10 +60,10 @@ while(True):
|
|||||||
if dets is not None:
|
if dets is not None:
|
||||||
for det in dets:
|
for det in dets:
|
||||||
mask = np.zeros((height, width, 3), dtype=np.uint8)
|
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))
|
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'):
|
if cv2.waitKey(5) & 0xFF == ord('q'):
|
||||||
break
|
break
|
||||||
|
@@ -68,8 +68,7 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
extern __declspec(dllexport) GoUintptr FindFaces(GoSlice pixels);
|
||||||
extern GoUintptr FindFaces(GoSlice p0);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user