-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-recognition-on-video-stream-extracting-one-frame-per-second-and-record-average-processing-time.cs
More file actions
116 lines (103 loc) · 3.64 KB
/
Copy pathrun-recognition-on-video-stream-extracting-one-frame-per-second-and-record-average-processing-time.cs
File metadata and controls
116 lines (103 loc) · 3.64 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
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Drawing;
/// <summary>
/// Demonstrates generating barcode images, treating them as video frames,
/// and measuring the time required to recognize barcodes in each frame.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Generates sample barcode frames, reads them, reports detection results,
/// calculates average processing time, and cleans up temporary files.
/// </summary>
static void Main()
{
// Number of frames to simulate (one per second)
const int frameCount = 5;
// Temporary directory for generated barcode images
string tempDir = Path.Combine(Path.GetTempPath(), "AsposeBarcodeVideoDemo");
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
// Generate sample barcode images to act as video frames
List<string> framePaths = new List<string>();
for (int i = 0; i < frameCount; i++)
{
// Build file path for the current frame
string filePath = Path.Combine(tempDir, $"frame_{i}.png");
// Create a barcode image with unique text for each frame
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, $"Sample{i}"))
{
generator.Save(filePath);
}
// Store the path for later processing
framePaths.Add(filePath);
}
// Variables for timing statistics
double totalMilliseconds = 0;
int processedFrames = 0;
// Process each generated frame
foreach (string framePath in framePaths)
{
// Verify the frame file exists before attempting to read it
if (!File.Exists(framePath))
{
Console.WriteLine($"Frame file not found: {framePath}");
continue;
}
// Start timing the recognition operation
var stopwatch = Stopwatch.StartNew();
// Read and decode all supported barcode types from the image
using (var reader = new BarCodeReader(framePath, DecodeType.AllSupportedTypes))
{
foreach (var result in reader.ReadBarCodes())
{
// Output detected barcode type and its text value
Console.WriteLine($"Detected [{result.CodeTypeName}]: {result.CodeText}");
}
}
// Stop timing and accumulate elapsed time
stopwatch.Stop();
totalMilliseconds += stopwatch.Elapsed.TotalMilliseconds;
processedFrames++;
}
// Report average recognition time if any frames were processed
if (processedFrames > 0)
{
double averageMs = totalMilliseconds / processedFrames;
Console.WriteLine($"Processed {processedFrames} frames. Average recognition time: {averageMs:F2} ms");
}
else
{
Console.WriteLine("No frames were processed.");
}
// Clean up temporary barcode image files
foreach (string path in framePaths)
{
try
{
File.Delete(path);
}
catch
{
// Ignore any deletion errors
}
}
// Attempt to delete the temporary directory
try
{
Directory.Delete(tempDir);
}
catch
{
// Ignore any deletion errors
}
}
}