Golang’s method of implementing image thumbnail generation and face detection
Abstract:
This article introduces the use of Golang to achieve image thumbnail generation and face detection method. First, we will generate thumbnails through Golang's image processing library and save the thumbnails to the local disk. Then, we will introduce how to use Golang's face detection library to detect faces in the generated thumbnails and return the detection results.
package main import ( "fmt" "image" "image/jpeg" "log" "os" "github.com/nfnt/resize" ) func main() { inFile, err := os.Open("input.jpg") if err != nil { log.Fatal(err) } defer inFile.Close() // Decode the image srcImg, _, err := image.Decode(inFile) if err != nil { log.Fatal(err) } // Resize the image thumbnail := resize.Resize(200, 0, srcImg, resize.Lanczos3) // Create a new file for the thumbnail outFile, err := os.Create("thumbnail.jpg") if err != nil { log.Fatal(err) } defer outFile.Close() // Encode the thumbnail to JPEG format err = jpeg.Encode(outFile, thumbnail, &jpeg.Options{jpeg.DefaultQuality}) if err != nil { log.Fatal(err) } fmt.Println("Thumbnail generated successfully!") }
This code first opens an image file named "input.jpg" and decodes it. Then, use the resize library to scale the image to a certain size (width 200 pixels in this example, height calculated automatically). After that, save the generated thumbnail to a file named "thumbnail.jpg". Finally, a prompt message indicating successful thumbnail generation is output.
package main import ( "fmt" "image" "image/jpeg" "log" "os" "github.com/esimov/stackblur-go" "github.com/Kagami/go-face" ) func main() { // Load the face detection model model, err := face.NewRecognizer("models") if err != nil { log.Fatal(err) } defer model.Close() // Open the thumbnail image file inFile, err := os.Open("thumbnail.jpg") if err != nil { log.Fatal(err) } defer inFile.Close() // Decode the thumbnail image srcImg, _, err := image.Decode(inFile) if err != nil { log.Fatal(err) } // Blur the image for better face detection results stackblur.Process(srcImg, uint32(srcImg.Bounds().Dx()), uint32(srcImg.Bounds().Dy()), 20) // Convert the image to grayscale grayImg, err := face.ConvertImageToGray(srcImg) if err != nil { log.Fatal(err) } // Detect faces in the image faces, err := model.Recognize(grayImg, 1.5, 3) if err != nil { log.Fatal(err) } fmt.Printf("Detected %d face(s) in the thumbnail ", len(faces)) // Draw rectangles around the detected faces for _, f := range faces { x, y, w, h := f.Rectangle() faceImg := face.Crop(grayImg, face.Rect(x, y, x+w, y+h)) outFile, err := os.Create("face.jpg") if err != nil { log.Fatal(err) } defer outFile.Close() // Encode the face image to JPEG format err = jpeg.Encode(outFile, faceImg, &jpeg.Options{jpeg.DefaultQuality}) if err != nil { log.Fatal(err) } fmt.Printf("Face detected at coordinates (%d,%d,%d,%d) ", x, y, w, h) } }
This code first loads the face detection model and opens a file called "thumbnail.jpg" Image files. Then, the thumbnails are blurred and grayscale converted to improve the accuracy of the face detection results. Next, use a face detection library to detect faces in the thumbnails and output the number of detected faces. Finally, the detected faces are marked in the form of rectangular boxes and saved to a file named "face.jpg".
Summary:
This article introduces how to use Golang to achieve image thumbnail generation and face detection. Through the support of third-party libraries, we can easily implement these functions in Golang. Using these techniques, we can process images and extract useful information from them, such as generating thumbnails and detecting faces. I hope this article can be helpful to you, thank you for reading!
The above is the detailed content of Golang implements image thumbnail generation and face detection methods. For more information, please follow other related articles on the PHP Chinese website!