提交 33c500c0 编写于 作者: P peterq

add: 未搜索到头像的使用文字头像 & add uncommitted files

上级 ceca972b
package cv
import (
"fmt"
"gocv.io/x/gocv"
"image"
"image/color"
"os"
"path"
"sync"
)
var xmlFile = "./data/haarcascades_cuda/haarcascade_frontalface_alt.xml"
var classifier = gocv.NewCascadeClassifier()
func init() {
if !classifier.Load(xmlFile) {
fmt.Printf("Error reading cascade file: %v\n", xmlFile)
return
}
}
func CV() {
deviceID := 0
// open webcam
webcam, err := gocv.VideoCaptureDevice(int(deviceID))
if err != nil {
fmt.Println(err)
return
}
defer webcam.Close()
// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
// color for the rect when faces detected
blue := color.RGBA{0, 0, 255, 0}
fmt.Printf("start reading camera device: %v\n", deviceID)
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
return
}
if img.Empty() {
continue
}
// detect faces
rects := classifier.DetectMultiScale(img)
fmt.Printf("found %d faces\n", len(rects))
// draw a rectangle around each face on the original image,
// along with text identifying as "Human"
for _, r := range rects {
gocv.Rectangle(&img, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}
var l = new(sync.Mutex)
func CheckFace(imgPath string, savePath string) []image.Rectangle {
l.Lock()
defer l.Unlock()
blue := color.RGBA{0, 0, 255, 0}
// 打开图片
img := gocv.IMRead(imgPath, -1)
defer img.Close()
// 进行识别
rects := classifier.DetectMultiScale(img)
if savePath != "" {
// 在图片中标记
for _, r := range rects {
gocv.Rectangle(&img, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
os.MkdirAll(path.Dir(savePath), os.ModePerm)
gocv.IMWrite(savePath, img)
}
return rects
}
file(GLOB HAAR_CASCADES haarcascades/*.xml)
file(GLOB LBP_CASCADES lbpcascades/*.xml)
install(FILES ${HAAR_CASCADES} DESTINATION ${OPENCV_OTHER_INSTALL_PATH}/haarcascades COMPONENT libs)
install(FILES ${LBP_CASCADES} DESTINATION ${OPENCV_OTHER_INSTALL_PATH}/lbpcascades COMPONENT libs)
if(INSTALL_TESTS AND OPENCV_TEST_DATA_PATH)
install(DIRECTORY "${OPENCV_TEST_DATA_PATH}/" DESTINATION "${OPENCV_TEST_DATA_INSTALL_PATH}" COMPONENT "tests")
endif()
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
This folder contains various data that is used by cv libraries and/or demo applications.
----------------------------------------------------------------------------------------
haarcascades - the folder contains trained classifiers for detecting objects
of a particular type, e.g. faces (frontal, profile), pedestrians etc.
Some of the classifiers have a special license - please,
look into the files for details.
......@@ -2,4 +2,8 @@ module github.com/peterq/pan-light/server/cmd
go 1.12
require gocv.io/x/gocv v0.20.0
require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
gocv.io/x/gocv v0.20.0
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
)
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
gocv.io/x/gocv v0.20.0 h1:2q75zQ8Zel2tB69G6qrmf/E7EdvaCs90qvkHzdSBOAg=
gocv.io/x/gocv v0.20.0/go.mod h1:vZETJRwLnl11muQ6iL3q4ju+0oJRrdmYdv5xJTH7WYA=
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff h1:+2zgJKVDVAz/BWSsuniCmU1kLCjL88Z8/kv39xCI9NQ=
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
......@@ -5,14 +5,19 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/peterq/pan-light/server/cmd/cv"
"image"
"image/color"
"image/draw"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/url"
......@@ -124,6 +129,7 @@ func handleItemResult(item *taskItem) {
return
}
log.Println("result", item.nickname, "fail")
textAvatar(item.nickname, savePath)
}
func searchImageLoop(nicknameChan chan *taskItem, imageChan chan *taskItem, wg *sync.WaitGroup) {
......@@ -167,9 +173,15 @@ func faceCheckLoop(imageChan chan *taskItem, resultChan chan *taskItem, wg *sync
var searchHttpClient http.Client
var faceHttpClient http.Client
var font *truetype.Font
func avatarInit() {
parseNicknameDoc()
bin, err := ioutil.ReadFile("./data/font/cn.ttf")
if err != nil {
panic(err)
}
font, err = freetype.ParseFont(bin)
jar, _ := cookiejar.New(nil)
jar1, _ := cookiejar.New(nil)
parallel := 20
......@@ -328,3 +340,89 @@ func downloadImg(imgPath string, link string) (img image.Image, err error) {
err = ioutil.WriteFile(imgPath, bin, os.ModePerm)
return
}
func textAvatar(nickname, savePath string) {
img := image.NewRGBA(image.Rect(0, 0, 300, 300))
rand.Seed(time.Now().UnixNano())
r, g, b, _ := hls(rand.Intn(360))
draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: 255,
}), image.ZP, draw.Src)
str := []rune(nickname)
if len(str) > 4 {
str = []rune{str[0]}
}
var x, y, fontSize int
ln1, ln2 := string(str), ""
if len(str) == 1 {
x, y, fontSize = 45, 700, img.Bounds().Dx()/3*2
} else if len(str) == 2 {
x, y, fontSize = 20, 700, img.Bounds().Dx()/2
} else if len(str) == 3 {
x, y, fontSize = 20, 700, img.Bounds().Dx()/3
} else {
ln1 = string(str[0]) + string(str[1])
ln2 = string(str[2]) + string(str[3])
x, y, fontSize = 60, 300, img.Bounds().Dx()/3
}
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(font)
c.SetFontSize(float64(fontSize))
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(image.NewUniform(color.White))
// Draw the text.
pt := freetype.Pt(x, x+int(c.PointToFixed(float64(y)))>>8)
pt1, err := c.DrawString(ln1, pt)
if err != nil {
log.Println(err)
}
pt.Y += (pt1.X-pt.X)/2 + c.PointToFixed(20)
_, err = c.DrawString(ln2, pt)
if err != nil {
log.Println(err)
}
os.MkdirAll(path.Dir(savePath), os.ModePerm)
buf := bytes.NewBuffer([]byte{})
jpeg.Encode(buf, img, nil)
ioutil.WriteFile(savePath, buf.Bytes(), os.ModePerm)
}
func hls(H int) (r, g, b, a int) {
S, V := 255, 255
// Direct implementation of the graph in this image:
// https://en.wikipedia.org/wiki/HSL_and_HSV#/media/File:HSV-RGB-comparison.svg
max := V
min := V * (255 - S)
H %= 360
segment := H / 60
offset := H % 60
mid := ((max - min) * offset) / 60
//log.Println(H, max, min, mid)
switch segment {
case 0:
return max, min + mid, min, 0xff
case 1:
return max - mid, max, min, 0xff
case 2:
return min, max, min + mid, 0xff
case 3:
return min, max - mid, max, 0xff
case 4:
return min + mid, min, max, 0xff
case 5:
return max, min, max - mid, 0xff
}
return 0, 0, 0, 0xff
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册