In [ ]:
# Uncomment line below when using Colab (this installs OpenCV4)
# %system SwiftCV/install/install_colab.sh
%install-location $cwd/swift-install
%install '.package(path: "$cwd/SwiftCV")' SwiftCV
%install '.package(path: "$cwd/FastaiNotebook_08_data_block")' FastaiNotebook_08_data_block
In [ ]:
%include "EnableIPythonDisplay.swift"
import Foundation
import SwiftCV
import Path
In [ ]:
import FastaiNotebook_08_data_block
In [ ]:
// display opencv version
print(cvVersion())
In [ ]:
func readImage(_ path:String)->Mat {
let cvImg = imread(path)
return cvtColor(cvImg, nil, ColorConversionCode.COLOR_BGR2RGB)
}
In [ ]:
let path = downloadImagenette(sz:"")
let allNames = fetchFiles(path: path/"train/n03425413", recurse: false, extensions: ["jpeg", "jpg"])
let fNames = Array(allNames[0..<256])
let ns = fNames.map {$0.string}
let imgpath = ns[2]
var cvImg = readImage(imgpath)
In [ ]:
cvImg.size
Out[ ]:
In [ ]:
print(type(of:cvImg.dataPtr))
In [ ]:
let ptr = UnsafeRawPointer(cvImg.dataPtr).assumingMemoryBound(to: UInt8.self)
In [ ]:
ptr[2]
Out[ ]:
In [ ]:
time(repeating:10) {_ = readImage(imgpath)}
In [ ]:
cvImg.rows
Out[ ]:
In [ ]:
import Python
import TensorFlow
In [ ]:
let plt = Python.import("matplotlib.pyplot")
let np = Python.import("numpy")
IPythonDisplay.shell.enable_matplotlib("inline")
Out[ ]:
In [ ]:
func show_img(_ img: Mat, _ w: Int = 7, _ h: Int = 5) {
show_img(Tensor<UInt8>(cvMat: img)!, w, h)
}
In [ ]:
show_img(cvImg)
In [ ]:
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_NEAREST)}
In [ ]:
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_LINEAR)}
In [ ]:
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)}
In [ ]:
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_AREA)}
In [ ]:
cvImg = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)
In [ ]:
func readResized(_ fn:String)->Mat {
return resize(readImage(fn), nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)
}
In [ ]:
var imgs = ns[0..<10].map(readResized)
In [ ]:
time(repeating:10) {_ = readResized(imgpath)}
In [ ]:
public protocol Countable {
var count:Int {get}
}
extension Mat :Countable {}
extension Array:Countable {}
public extension Sequence where Element:Countable {
var totalCount:Int { return map{ $0.count }.reduce(0, +) }
}
In [ ]:
func collateMats(_ imgs:[Mat])->Tensor<Float> {
let c = imgs.totalCount
let ptr = UnsafeMutableRawPointer.allocate(byteCount: c, alignment: 1)
defer {ptr.deallocate()}
var p = ptr
for img in imgs {
p.copyMemory(from: img.dataPtr, byteCount: img.count)
p += img.count
}
let r = UnsafeBufferPointer(start: ptr.bindMemory(to: UInt8.self, capacity: c), count: c)
cvImg = imgs[0]
let shape = TensorShape([imgs.count, cvImg.rows, cvImg.cols, cvImg.channels])
let res = Tensor(shape: shape, scalars: r)
return Tensor<Float>(res)/255.0
}
In [ ]:
var t = collateMats(imgs)
In [ ]:
t.shape
Out[ ]:
In [ ]:
show_img(t[2])
In [ ]:
time(repeating:10) {_ = collateMats(imgs)}
In [ ]:
time { _ = ns.map(readResized) }
In [ ]:
show_img(
resize(cvImg, nil, Size(100, 50), 0, 0, InterpolationFlag.INTER_AREA)
)
In [ ]:
let zoomMat = getRotationMatrix2D(Size(cvImg.cols, cvImg.rows / 2), 0, 1)
show_img(
warpAffine(cvImg, nil, zoomMat, Size(600, 600))
)
In [ ]:
let rotMat = getRotationMatrix2D(Size(cvImg.cols / 2, cvImg.rows / 2), 20, 1)
show_img(
warpAffine(cvImg, nil, rotMat, Size(cvImg.cols, cvImg.rows))
)
In [ ]:
show_img(
copyMakeBorder(cvImg, nil, 40, 40, 40, 40, BorderType.BORDER_CONSTANT, RGBA(0, 127, 0, 0))
)
In [ ]:
show_img(
GaussianBlur(cvImg, nil, Size(25, 25))
)
In [ ]:
show_img(
flip(cvImg, nil, FlipMode.HORIZONTAL)
)
In [ ]:
show_img(
transpose(cvImg, nil)
)
In [ ]: