/
main.go
98 lines (81 loc) · 2.22 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"image"
_ "image/png"
"io/ioutil"
"log"
"os"
"github.com/otiai10/gosseract"
)
const (
thresh = 0.75
)
func blackPixelPercent(im image.Image) float64 {
bounds := im.Bounds()
// An image's bounds do not necessarily start at (0, 0), so the two loops start
// at bounds.Min.Y and bounds.Min.X. Looping over Y first and X second is more
// likely to result in better memory access patterns than X first and Y second.
pixelCount := (bounds.Max.Y - bounds.Min.Y) * (bounds.Max.X - bounds.Min.X)
blackPixels := 0
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := im.At(x, y).RGBA()
if r == 0 && g == 0 && b == 0 {
blackPixels++
}
}
}
return float64(blackPixels) / float64(pixelCount)
}
func loadImage(s string) (image.Image, error) {
reader, err := os.Open(s)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
// reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
m, _, err := image.Decode(reader)
return m, err
}
type episode struct {
cards []string
title string
}
func main() {
// Decode the JPEG data. If reading from file, create a reader with
episodesData := []episode{}
season := "data/frames/16/"
// names of folders of episodes
episodeDirs, _ := ioutil.ReadDir(season)
// each episode
for e := range episodeDirs {
if episodeDirs[e].IsDir() {
// names of files
episodeName := episodeDirs[e].Name()
episodeDir, _ := ioutil.ReadDir(season + episodeName)
ed := episode{
title: episodeName,
}
for i := range episodeDir {
m, err := loadImage(season + episodeName + "/" + episodeDir[i].Name())
if err != nil {
log.Fatal("Error loading image.")
}
blackPixels := blackPixelPercent(m)
if blackPixels > thresh {
out := gosseract.Must(gosseract.Params{Src: season + episodeName + "/" + episodeDir[i].Name(), Languages: "eng"})
if out != "" {
// fmt.Println("candidate frame", episodeDir[i].Name())
ed.cards = append(ed.cards, out)
// fmt.Println(out)
}
}
}
episodesData = append(episodesData, ed)
}
}
s, _ := json.Marshal(map[string][]episode{"episodes": episodesData})
fmt.Println(string(s))
}