func Handle(req []byte) string {
	var (
		resp  DetectionResult
		rects []image.Rectangle
		data  []byte
		image []byte
	)
	if val, exists := os.LookupEnv("input_mode"); exists && val == "url" {
		inputURL := strings.TrimSpace(string(req))
		res, err := http.Get(inputURL)
		if err != nil {
			return fmt.Sprintf("Unable to download image file from URI: %s, status %v", inputURL, res.Status)
		}
		defer res.Body.Close()
		data, err = ioutil.ReadAll(res.Body)
		if err != nil {
			return fmt.Sprintf("Unable to read response body: %s", err)
		}
	} else {
		var decodeError error
		data, decodeError = base64.StdEncoding.DecodeString(string(req))
		if decodeError != nil {
			data = req
		}
		contentType := http.DetectContentType(req)
		if contentType != "image/jpeg" && contentType != "image/png" {
			return fmt.Sprintf("Only jpeg or png images, either raw uncompressed bytes or base64 encoded are acceptable inputs, you uploaded: %s", contentType)
		}
	}
	tmpfile, err := ioutil.TempFile("/tmp", "image")
	if err != nil {
		log.Fatalf("Unable to create temporary file: %v", err)
	}
	defer os.Remove(tmpfile.Name())
	_, err = io.Copy(tmpfile, bytes.NewBuffer(data))
	if err != nil {
		return fmt.Sprintf("Unable to copy the source URI to the destionation file")
	}
	var output string
	query, err := url.ParseQuery(os.Getenv("Http_Query"))
	if err == nil {
		output = query.Get("output")
	}
	if val, exists := os.LookupEnv("output_mode"); exists {
		output = val
	}
	fd := NewFaceDetector("./data/facefinder", 20, 2000, 0.1, 1.1, 0.18)
	faces, err := fd.DetectFaces(tmpfile.Name())
	if err != nil {
		return fmt.Sprintf("Error on face detection: %v", err)
	}
	if output == "image" || output == "json_image" {
		var err error
		rects, image, err = fd.DrawFaces(data, faces)
		if err != nil {
			return fmt.Sprintf("Error creating image output: %s", err)
		}
		resp = DetectionResult{
			Faces:       rects,
			ImageBase64: base64.StdEncoding.EncodeToString(image),
		}
	}
	if output == "image" {
		return string(image)
	}
	j, err := json.Marshal(resp)
	if err != nil {
		return fmt.Sprintf("Error encoding output: %s", err)
	}
	return string(j)
}
Handle a serverless request
package function
import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"image"
	"image/jpeg"
	"io"
	"io/ioutil"
	"log"
	"math"
	"net/http"
	"net/url"
	"os"
	"strings"
	"time"
	"github.com/esimov/pigo/core"
	"github.com/esimov/stackblur-go"
	"github.com/fogleman/gg"
)
var dc *gg.Context
type FaceDetector struct {
	cascadeFile  string
	minSize      int
	maxSize      int
	shiftFactor  float64
	scaleFactor  float64
	iouThreshold float64
}
type DetectionResult struct {
	Faces       []image.Rectangle
	ImageBase64 string
}