Forwarded from dnaugsuz
好奇怪,Kotlin
Array<reified T> 就不可以引用 ::class.java 了 😂java.reflect.Array.newInstance(Class::class.java, n).javaClass 用 java.reflect 这个的确是标准答案Forwarded from 蛋挞观察室
Forwarded from Mivik Q
毕竟jvm有一个泛型抹平在那里,是没法获取到泛型特化后的“类“的。而java对数组做了特殊处理,kt只是把数组当成一个泛型了
Forwarded from dnaugsuz
所以说下面 null 的那个因为是 instance 级别的反射所以能拿到
Class<T>?This media is not supported in your browser
VIEW IN TELEGRAM
草,总算可以正常写入了,发现是没有提前转换 array => UMat 的缘故
Forwarded from dnaugsuz
后来我发现
这个应该支持读入 srt ,所以准确的做法是加 srt 解析库,再弄一个
expandRangeStart 简直荒谬,因为那个是用来合并 extract-subtitle 的扫描结果的(按帧算)……这个应该支持读入 srt ,所以准确的做法是加 srt 解析库,再弄一个
frameNoFromMillis 然后直接循环赋值,不需要 sort + zipWithNextclass srt.Subtitle(index, start, end, content, proprietary='')
def srt.parse(text: str) -> Subtitle
🤔还怪好用的,这就去写个 expandSubtitles #Python #CV
OpenCV 读单帧
imread(path) 还 imwrite(path ,img) 还有 imshow(title, img) 读 video 用
VideoCapture(path) 可以 unfinished, img = read() 和 release() 写 video 用
VideoWriter(path, VideoWriter.fourcc(*"X264"), fps, size)
方便使用 img[x, y, w, h] 进行 crop,使用 cv2.cvtColor(img, cv2.BGR2GRAY) 灰度cv2.inRange bandpass filter (binarize: 2 value) 还有 cv2.merge 位运算取得通道还
cv2.absdiff 来算帧差,和 numpy.ndarray 无缝交互我正考虑可不可以利用 Py 的动态类型给
cv.UMat 做个 PIL.Image 的包装 adapter
duangsuse::Echo
OpenCV 的 VideoWriter 真是无聊,本来以为 fourcc 设计也就是个 4*len(ascii) 不能算嘈点,结果静默失败,我调用了 write 结果输出个空文件,还不能当参数传不然莫名其妙返回什么 NULL 错误
[DuangSUSE@duangsuse]~/Projects/MontagePY% ./montage1_c.py ../extract-subtitles/extract-subtitles/Gswx.mp4 --subtitle ../extract-subtitles/extract-subtitles/HxN.srt -scale 2.0 -font-size 30 -spacing :-15,-15 -font /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc
30px, (255, 255, 255) ±20 (-15, -15)
20fps*6643 540x360
#fxxk 😂 正当 duangsuse 疯狂吐嘈
VideoWriter 不能用很久之后,才发现原来是自己的 -scale 参数和 write() 不兼容,构造器参数写错了不过,既然别人编程都有问题,OpenCV 连个 fatal 都没有,真是太浪费时间了。还废了我好长时间怀疑是 GC 和 use-after-free,差点把重构的面向对象结构毁掉。
./montage1_c.py ../extract-subtitles/extract-subtitles/Gswx.mp4 --subtitle ../extract-subtitles/extract-subtitles/HxN.srt -font-size 30 -spacing :-15,-15 -font /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc -scale 2.0 -key-color '000000' --key-thres 20
./montage1_c.py ../extract-subtitles/extract-subtitles/Gswx.mp4 -font /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc -scale 2.0 -key-color '000000' --key-thres 20 -text █ -font-size 20 -spacing :-10,-12 现在 srt 字幕展开已经可以用了,两个视频。
def expandSrts(srts, fps, count, placeholder="#"):
indexed = [placeholder for _ in range(count)]
no = lambda t: int(t.total_seconds() * fps)
for srt in srts:
start, end = no(srt.start), no(srt.end)
indexed[start:end] = repeat(srt.content, end - start)
return indexed
因为有了
datetime.deltatime ,所以 fps 计算变得直白了起来,只需要 total_seconds()*fps 即可 #Python #CV #codedef solveItemLayout(size, item_size, scale, spacing):
(width, height) = size
(w_item, h_item) = tuple((sz+sp)*scale for (sz, sp) in zip(item_size, spacing))
(m_item, n_item) = tuple(int(v) for v in [width / w_item, height / h_item])
(padLeft, padTop) = tuple(int(sz*scale / 4) for sz in [(width % w_item), (height % h_item)])
return (w_item, h_item, m_item, n_item, padLeft, padTop)
#Python #CV #code 经过修复了的
solveItemLayout 现在只计算长宽、个数和 padding 了,这些结果可以被缓存以提升效率因为最终直接要被
solveItemColors 解析位置和颜色,不必先 resize 大(scale) 再 resize 小(提取均色),我删掉了主逻辑处的 resizedef solveItemColors(img, layout):最后的绘制逻辑相当简单,我删掉了从 color tuple 转到 HTML color 的操作,因为它好像能接受。
(width, height) = img.size
(w_item, h_item, m_item, n_item, padLeft, padTop) = layout
img_average = img.resize((m_item, n_item), Image.BICUBIC, box=(padLeft, padTop, img.width-padLeft, img.height-padTop))
for i in range(0, n_item):
for j in range(0, m_item):
(y, x) = (padTop + i*h_item, padLeft + j*w_item)
yield (x, y, img_average.getpixel((j, i)) )
def drawTextMontage(img, areas, seq, font, calc_draw_color):
draw = ImageDraw.Draw(img)
for (x, y, color) in areas:
drawc = calc_draw_color(color)
if drawc != None:
draw.text((x, y), next(seq), font=font, fill=(drawc))
总体来看分成求布局、求xy和颜色、作画三个子程序,参数
font+size, scale, spacing, key_color, text ,如果希望了解更多代码请到这里查看Telegram
duangsues.is_a? SaltedFish
# font, font_size, scale, spacing; key_color
class Montage:
def __init__(self, cfg, size):
self.font = cfg.font; self.scale = cfg.scale; self.spacing = cfg.spacing
self.text = cfg.text
self.key_color = cfg.key_color; self.calc_draw_color = …
class Montage:
def __init__(self, cfg, size):
self.font = cfg.font; self.scale = cfg.scale; self.spacing = cfg.spacing
self.text = cfg.text
self.key_color = cfg.key_color; self.calc_draw_color = …