From 27c9e48a33c85380e32a248bcace586a7dcc757b Mon Sep 17 00:00:00 2001 From: esimov Date: Fri, 22 Feb 2019 17:32:37 +0200 Subject: [PATCH] Remove delaunay example ATM --- examples/delaunay/face_triangulator.py | 79 ---------- examples/delaunay/pigo.go | 206 ------------------------- examples/delaunay/pigo.h | 72 --------- 3 files changed, 357 deletions(-) delete mode 100644 examples/delaunay/face_triangulator.py delete mode 100644 examples/delaunay/pigo.go delete mode 100644 examples/delaunay/pigo.h diff --git a/examples/delaunay/face_triangulator.py b/examples/delaunay/face_triangulator.py deleted file mode 100644 index d130058..0000000 --- a/examples/delaunay/face_triangulator.py +++ /dev/null @@ -1,79 +0,0 @@ -#### WORK IN PROGRESS!!!!!!!!!! -from ctypes import * - -import subprocess -import numpy as np -import os -import cv2 -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 - -# define class GoPixelSlice to map to: -# C type struct { void *data; GoInt len; GoInt cap; } -class GoPixelSlice(Structure): - _fields_ = [ - ("pixels", POINTER(c_ubyte)), ("len", c_longlong), ("cap", c_longlong), - ] - -# Obtain the camera pixels and transfer them to Go trough Ctypes. -def process_frame(pixs): - dets = np.zeros(3 * MAX_NDETS, dtype=np.float32) - pixels = cast((c_ubyte * len(pixs))(*pixs), POINTER(c_ubyte)) - - # call FindFaces - faces = GoPixelSlice(pixels, len(pixs), len(pixs)) - pigo.FindFaces.argtypes = [GoPixelSlice] - pigo.FindFaces.restype = c_void_p - - # Call the exported FindFaces function from Go. - 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,)) - - # 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] - - 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].reshape((frame.shape[0], frame.shape[1]))) - pixs = pixs.flatten() - - # Verify if camera is intialized by checking if pixel array is not empty. - if np.any(pixs): - dets = process_frame(pixs) # pixs needs to be np.uint8 array - if dets is not None: - for det in dets: - #cv2.floodFill - mask = np.zeros((height, width, 3), dtype=np.uint8) - mask = cv2.circle(mask, (int(det[1]), int(det[0])), int(det[2]/2.0), np.array([255, 255, 255]), -1) - frame = np.where(mask!=np.array([255, 255, 255]), frame, cv2.blur(frame, (30, 30), 0)) - - cv2.imshow('', frame) - - if cv2.waitKey(5) & 0xFF == ord('q'): - break - -cap.release() -cv2.destroyAllWindows() diff --git a/examples/delaunay/pigo.go b/examples/delaunay/pigo.go deleted file mode 100644 index f11d045..0000000 --- a/examples/delaunay/pigo.go +++ /dev/null @@ -1,206 +0,0 @@ -// WORK IN PROGRESS!!!! -package main - -import "C" - -import ( - "image" - "image/color" - "io/ioutil" - "log" - "runtime" - "unsafe" - - "github.com/esimov/pigo/core" - "github.com/esimov/triangle" -) - -var ( - cascade []byte - err error - p *pigo.Pigo - classifier *pigo.Pigo -) - -type SubImager interface { - SubImage(r image.Rectangle) image.Image -} - -type pixs struct { - rows, cols int -} - -func main() {} - -//export FindFaces -func FindFaces(pixels []uint8) uintptr { - px := &pixs{ - rows: 480, - cols: 640, - } - - proc := &triangle.Processor{ - BlurRadius: 4, - SobelThreshold: 10, - PointsThreshold: 20, - MaxPoints: 100, - Wireframe: 0, - Noise: 0, - StrokeWidth: 1, - IsSolid: false, - Grayscale: false, - OutputToSVG: false, - OutputInWeb: false, - } - tri := &triangle.Image{*proc} - - pointCh := make(chan uintptr) - - dets := px.clusterDetection(pixels) - img := px.pixToImage(pixels) - - tFaces := make([][]int, len(dets)) - result := make([][]int, len(dets)) - det := make([]int, 0, len(dets)) - - totalPixDim := 0 - - go func() { - for i := 0; i < len(dets); i++ { - if dets[i].Q >= 5.0 { - result[i] = append(result[i], dets[i].Row, dets[i].Col, dets[i].Scale) - rect := image.Rect( - dets[i].Col-dets[i].Scale/2, - dets[i].Row-dets[i].Scale/2, - dets[i].Scale, - dets[i].Scale, - ) - - subImg := img.(SubImager).SubImage(rect) - bounds := subImg.Bounds() - - if bounds.Dx() > 1 && bounds.Dy() > 1 { - triRes, _, _, err := tri.Draw(subImg, false, func() {}) - if err != nil { - log.Fatal(err.Error()) - } - triPix := px.imgToPix(triRes) - tFaces[i] = append(tFaces[i], triPix...) - - // Prepend the top left coordinates of the detected faces to the delaunay triangles. - tFaces[i] = append([]int{len(triPix), bounds.Min.X, bounds.Min.Y}, tFaces[i]...) - totalPixDim += len(triPix) - } - } - } - - // Since in Go we cannot transfer a 2d array trough an array pointer - // we have to transform it into 1d array. - for _, v := range result { - det = append(det, v...) - } - - // Convert the multidimmensional slice containing the triangulated images to 1d slice. - convTri := make([]int, 0, len(result)+totalPixDim) - for _, v := range tFaces { - convTri = append(convTri, v...) - } - - // Include as a first slice element the number of detected faces. - // We need to transfer this value in order to define the Python array buffer length. - det = append([]int{len(result), 0, 0}, det...) - - // Append the generated triangle slices to detected faces array. - det = append(det, convTri...) - - // Convert the slice into an array pointer. - s := *(*[]int)(unsafe.Pointer(&det)) - p := uintptr(unsafe.Pointer(&s[0])) - - // Ensure `det` is not freed up by GC prematurely. - runtime.KeepAlive(det) - - // return the pointer address - pointCh <- p - }() - return <-pointCh -} - -// clusterDetection runs Pigo face detector core methods -// and returns a cluster with the detected faces coordinates. -func (px pixs) clusterDetection(pixels []uint8) []pigo.Detection { - cParams := pigo.CascadeParams{ - MinSize: 20, - MaxSize: 1000, - ShiftFactor: 0.15, - ScaleFactor: 1.1, - ImageParams: pigo.ImageParams{ - Pixels: pixels, - Rows: px.rows, - Cols: px.cols, - Dim: px.cols, - }, - } - - if len(cascade) == 0 { - cascade, err = ioutil.ReadFile("../../data/facefinder") - if err != nil { - log.Fatalf("Error reading the cascade file: %v", err) - } - - // Unpack the binary file. This will return the number of cascade trees, - // the tree depth, the threshold and the prediction from tree's leaf nodes. - classifier, err = p.Unpack(cascade) - if err != nil { - log.Fatalf("Error reading the cascade file: %s", err) - } - } - - // Run the classifier over the obtained leaf nodes and return the detection results. - // The result contains quadruplets representing the row, column, scale and detection score. - dets := classifier.RunCascade(cParams, 0.0) - - // Calculate the intersection over union (IoU) of two clusters. - dets = classifier.ClusterDetections(dets, 0) - - return dets -} - -// pixToImage converts the pixel array to an image. -func (px pixs) pixToImage(pixels []uint8) image.Image { - width, height := px.cols, px.rows - img := image.NewRGBA(image.Rect(0, 0, width, height)) - c := color.RGBA{ - R: uint8(0), - G: uint8(0), - B: uint8(0), - A: uint8(255), - } - - for y := 0; y < height; y++ { - for x := 0; x < width; x++ { - c.B = uint8(pixels[y*3+x]) - c.G = uint8(pixels[y*3+x+1]) - c.R = uint8(pixels[y*3+x+2]) - - img.SetRGBA(x, y, c) - } - } - return img -} - -// imgToPix converts the image to a pixel array. -func (px pixs) imgToPix(img image.Image) []int { - bounds := img.Bounds() - x := bounds.Dx() - y := bounds.Dy() - pixels := make([]int, 0, x*y*3) - - for i := bounds.Min.X; i < bounds.Max.X; i++ { - for j := bounds.Min.Y; j < bounds.Max.Y; j++ { - r, g, b, _ := img.At(i, j).RGBA() - pixels = append(pixels, int(r>>8), int(g>>8), int(b>>8), 255) - } - } - return pixels -} diff --git a/examples/delaunay/pigo.h b/examples/delaunay/pigo.h deleted file mode 100644 index e188834..0000000 --- a/examples/delaunay/pigo.h +++ /dev/null @@ -1,72 +0,0 @@ -/* Created by "go tool cgo" - DO NOT EDIT. */ - -/* package command-line-arguments */ - - -#line 1 "cgo-builtin-prolog" - -#include /* for ptrdiff_t below */ - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -typedef struct { const char *p; ptrdiff_t n; } _GoString_; - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef __SIZE_TYPE__ GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -typedef _GoString_ GoString; -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - - -extern GoUintptr FindFaces(GoSlice p0); - -#ifdef __cplusplus -} -#endif