-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (169 loc) · 5.4 KB
/
Copy pathmain.go
File metadata and controls
209 lines (169 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"image"
"image/color"
"log"
"math"
"os"
"strconv"
"time"
"gocv.io/x/gocv"
)
const (
frameBufferSize = 110 // Buffer size to hold past frames
maxCameraRetries = 5 // Maximum number for exponential read retries
minBlendOffset = 1 // Minimum delay
maxBlendOffset = 109 // Maximum delay
)
var faceDetectColor = color.RGBA{0, 255, 0, 70}
var fpsCounterColor = color.RGBA{0, 255, 0, 0}
type FPSCalculator struct {
frameCount int
startTime time.Time
fps float64
}
func NewFPSCalculator() *FPSCalculator {
return &FPSCalculator{
startTime: time.Now(),
}
}
func (fpsCalc *FPSCalculator) calculateFPS() float64 {
fpsCalc.frameCount++
elapsedTime := time.Since(fpsCalc.startTime).Seconds()
if elapsedTime > 1.0 {
fpsCalc.fps = float64(fpsCalc.frameCount) / elapsedTime
fpsCalc.frameCount = 0
fpsCalc.startTime = time.Now()
}
return fpsCalc.fps
}
func main() {
// defer profile.Start(profile.MemProfile).Stop()
if len(os.Args) < 2 {
fmt.Println("How to run:\n\n\tgosvm [camera ID]")
return
}
// Parse args
deviceID, _ := strconv.Atoi(os.Args[1])
// Open webcam
webcam, err := gocv.VideoCaptureDevice(int(deviceID))
if err != nil {
fmt.Println(err)
return
}
defer webcam.Close()
// Open display window
window := gocv.NewWindow("Motion Extraction")
defer window.Close()
// Prepare a buffer to hold past frames
frameBuffer := make([]gocv.Mat, frameBufferSize)
for i := range frameBuffer {
frameBuffer[i] = gocv.NewMat()
}
// Initialize FPS calculator
fpsCalculator := NewFPSCalculator()
fmt.Printf("start reading camera device: %v\n", deviceID)
// Number of frames to delay when blending
blendOffset := 3
// Current frame index inside the frameBuffer
currentIndex := 0
for {
currentFrame := gocv.NewMat()
if !ReadWebcamWithRetry(webcam, ¤tFrame, maxCameraRetries) {
log.Fatal("Failed to read from webcam after multiple attempts")
break
}
if currentFrame.Empty() {
currentFrame.Close()
break
}
// Close the Mat at the current buffer position to avoid memory leaks
if frameBuffer[currentIndex].Empty() == false {
frameBuffer[currentIndex].Close()
}
// Denoise captured image
gocv.FastNlMeansDenoisingColoredWithParams(currentFrame, ¤tFrame, 28.0, 12.0, 12, 7)
// Store the current frame in the buffer
frameBuffer[currentIndex] = currentFrame.Clone()
currentFrame.Close()
// Determine the frame to blend with based on the desired delay
blendIndex := (currentIndex - blendOffset + frameBufferSize) % frameBufferSize
blendFrame := frameBuffer[blendIndex]
originalFrame := frameBuffer[currentIndex]
if !blendFrame.Empty() {
// Create a half-transparent inverted version of the frame to blend
halfTransparentFrame := gocv.NewMat()
// Invert the frame
gocv.BitwiseNot(blendFrame, &halfTransparentFrame)
gocv.AddWeighted(halfTransparentFrame, 0.5, originalFrame, 0.0, 0, &halfTransparentFrame)
// Blend the current frame with the delayed frame
blendedFrame := gocv.NewMat()
blendFrames(originalFrame, halfTransparentFrame, &blendedFrame, 0.4)
// Apply emobss effect
applyEmbossEffect(blendedFrame, &blendedFrame)
// Calculate FPS
if fpsCalculator != nil {
fps := fpsCalculator.calculateFPS()
gocv.PutText(&blendedFrame, fmt.Sprintf("FPS: %.2f, Delay: %d (A/D keys to inc/dec)", fps, blendOffset), image.Pt(10, 40), gocv.FontHersheyPlain, 1.9, fpsCounterColor, 2)
}
// Display the resulting frame in the window
window.IMShow(blendedFrame)
// Close Mats manualy
blendedFrame.Close()
halfTransparentFrame.Close()
}
// Move to the next index in the circular buffer
currentIndex = (currentIndex + 1) % frameBufferSize
// Handle user input to change the blend offset
key := window.WaitKey(10)
if key == 27 || key == 113 { // ESC or Q key to exit
break
} else if key == 97 { // "A" key decreses blendOffset
if blendOffset > minBlendOffset {
blendOffset--
}
} else if key == 100 { // "D" key increses blendOffset
if blendOffset < maxBlendOffset {
blendOffset++
}
}
time.Sleep(30 * time.Millisecond)
}
}
// ReadWithRetry attempts to read from the webcam with exponential backoff retries
func ReadWebcamWithRetry(webcam *gocv.VideoCapture, img *gocv.Mat, maxRetries int) bool {
var delay time.Duration
for attempt := 0; attempt <= maxRetries; attempt++ {
if webcam.Read(img) {
return true
}
if img.Empty() {
delay = time.Duration(math.Pow(2, float64(attempt))) * time.Millisecond
fmt.Printf("Retrying in %v...\n", delay)
time.Sleep(delay)
}
}
return false
}
// blendFrames blends two frames with a specified alpha value
func blendFrames(frame1, frame2 gocv.Mat, result *gocv.Mat, alpha float64) {
gocv.AddWeighted(frame1, alpha, frame2, 1-alpha, 0, result)
}
// applyEmbossEffect applies an emboss effect to the input frame
func applyEmbossEffect(input gocv.Mat, output *gocv.Mat) {
// Define an emboss kernel
kernel := gocv.NewMatWithSizeFromScalar(gocv.NewScalar(0, 0, 0, 0), 3, 3, gocv.MatTypeCV32F)
defer kernel.Close()
kernel.SetFloatAt(0, 0, -2)
kernel.SetFloatAt(0, 1, -1)
kernel.SetFloatAt(0, 2, 0)
kernel.SetFloatAt(1, 0, -1)
kernel.SetFloatAt(1, 1, 1)
kernel.SetFloatAt(1, 2, 1)
kernel.SetFloatAt(2, 0, 0)
kernel.SetFloatAt(2, 1, 1)
kernel.SetFloatAt(2, 2, 2)
// Apply the emboss kernel to the input image
gocv.Filter2D(input, output, -1, kernel, image.Pt(-1, -1), 0.0, gocv.BorderReflect)
}