-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognition.go
More file actions
122 lines (104 loc) · 3.09 KB
/
recognition.go
File metadata and controls
122 lines (104 loc) · 3.09 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
package hearnoevil
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/IBM/go-sdk-core/core"
"github.com/mikerourke/forensic-files-api/internal/crimeseen"
"github.com/mikerourke/forensic-files-api/internal/visibilityzero"
"github.com/mikerourke/forensic-files-api/internal/whodunit"
"github.com/sirupsen/logrus"
stv1 "github.com/watson-developer-cloud/go-sdk/speechtotextv1"
)
// Recognition represents a speech-to-text service job.
type Recognition struct {
*whodunit.Episode
}
// NewRecognition returns a new instance of recognition.
func NewRecognition(ep *whodunit.Episode) *Recognition {
return &Recognition{
Episode: ep,
}
}
// StartJob starts a new recognition job.
func (r *Recognition) StartJob(stt *s2tInstance, callbackURL string) {
if r.Exists() {
log.WithField("file", r.FileName()).Infoln(
"Skipping job, already exists")
return
}
a := visibilityzero.NewAudio(r.Episode)
if !a.Exists() {
log.WithField("file", a.FileName()).Warnln(
"Skipping job, audio file not found")
return
}
audio := a.Open()
if audio == nil {
return
}
log.WithFields(logrus.Fields{
"season": r.SeasonNumber,
"episode": r.EpisodeNumber,
}).Infoln("Creating Recognition job")
_, _, err := stt.CreateJob(r.jobOptions(audio, callbackURL))
if err != nil {
log.WithError(err).Errorln("Error creating job")
return
}
log.Infoln("Job successfully created")
}
func (r *Recognition) jobOptions(
audio *os.File,
callbackURL string,
) *stv1.CreateJobOptions {
return &stv1.CreateJobOptions{
Audio: audio,
ContentType: core.StringPtr("audio/mp3"),
CallbackURL: core.StringPtr(callbackURL),
UserToken: core.StringPtr(r.Name()),
Events: core.StringPtr("recognitions.completed_with_results"),
ProfanityFilter: core.BoolPtr(false),
SmartFormatting: core.BoolPtr(true),
}
}
// WriteResults writes the specified contents to a new JSON file in the
// `/recognitions` directory.
func (r *Recognition) WriteResults(contents interface{}) error {
path := r.AssetFilePath(whodunit.AssetTypeRecognition)
return crimeseen.WriteJSONFile(path, contents)
}
// ReadResults returns the results from the recognition JSON file.
func (r *Recognition) ReadResults() (
[]stv1.SpeechRecognitionResult,
error,
) {
path := r.FilePath()
jsonFile, err := os.Open(path)
if err != nil {
return nil, err
}
defer jsonFile.Close()
bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
var contents []stv1.SpeechRecognitionResults
err = json.Unmarshal(bytes, &contents)
if err != nil {
return nil, err
}
return contents[0].Results, nil
}
// Exists return true if the recognition file exists in the `/assets` directory.
func (r *Recognition) Exists() bool {
return r.AssetExists(whodunit.AssetTypeRecognition)
}
// FilePath returns the path to the recognition file in the `/assets` directory.
func (r *Recognition) FilePath() string {
return r.AssetFilePath(whodunit.AssetTypeRecognition)
}
// FileName returns the name of the audio file in the `/assets` directory.
func (r *Recognition) FileName() string {
return r.AssetFileName(whodunit.AssetTypeRecognition)
}