-
Notifications
You must be signed in to change notification settings - Fork 86
added cropping of capture/background #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
127009b
65627e8
7b41672
cb0fe4f
3eeb58b
01e9a75
f81e26e
112e979
22472ad
7dc9bcc
6894b3e
1769f7e
e9a5242
0f62a57
81c1c83
4c5f72c
ffb9883
7353ff8
5a92b93
5ee0d4a
44dbb6a
4c6431e
975b96f
481cb47
f80a8a6
ea1e350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -372,6 +372,7 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
bool flipVertical = false; | ||||||||||||||||||||||
int fourcc = 0; | ||||||||||||||||||||||
size_t blur_strength = 0; | ||||||||||||||||||||||
cv::Rect_<int> crop_region(0,0,0,0); | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
const char* modelname = "selfiesegmentation_mlkit-256x256-2021_01_19-v1215.f16.tflite"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -568,6 +569,15 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
if (expWidth != vidGeo.value().first) { | ||||||||||||||||||||||
fprintf(stderr, "Warning: virtual camera aspect ratio does not match capture device.\n"); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// calculate crop region, only if result always smaller | ||||||||||||||||||||||
if (expWidth != vidGeo.value().first && | ||||||||||||||||||||||
vidGeo.value().first <= capGeo.value().first && | ||||||||||||||||||||||
vidGeo.value().second <= capGeo.value().second) { | ||||||||||||||||||||||
crop_region = calcCropping(capGeo.value().first, | ||||||||||||||||||||||
capGeo.value().second, | ||||||||||||||||||||||
vidGeo.value().first, | ||||||||||||||||||||||
vidGeo.value().second); | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// dump settings.. | ||||||||||||||||||||||
printf("debug: %d\n", debug); | ||||||||||||||||||||||
|
@@ -600,7 +610,12 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// default green screen background (at capture true geometry) | ||||||||||||||||||||||
cv::Mat bg = cv::Mat(capGeo.value().second, capGeo.value().first, CV_8UC3, cv::Scalar(0, 255, 0)); | ||||||||||||||||||||||
cv::Mat bg; | ||||||||||||||||||||||
if ( crop_region.height == 0) { | ||||||||||||||||||||||
bg = cv::Mat(capGeo.value().second, capGeo.value().first, CV_8UC3, cv::Scalar(0, 255, 0)); | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
bg = cv::Mat(crop_region.width, crop_region.height, CV_8UC3, cv::Scalar(0, 255, 0)); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DRY:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a mistake here! I must check this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not okay:
gcc 12.2.1 is not happy, and try to assign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hoped the compiler can resolve the initializer list, but you might need to make it explicit as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tried to solve this, but there is no advantage compared to my first code, only more code. |
||||||||||||||||||||||
|
||||||||||||||||||||||
// Virtual camera (at specified geometry) | ||||||||||||||||||||||
int lbfd = loopback_init(s_vcam, vidGeo.value().first, vidGeo.value().second, debug); | ||||||||||||||||||||||
|
@@ -615,9 +630,26 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
// Processing components, all at capture true geometry | ||||||||||||||||||||||
cv::Mat mask(capGeo.value().second, capGeo.value().first, CV_8U); | ||||||||||||||||||||||
cv::Mat mask; | ||||||||||||||||||||||
if ( crop_region.height ) { | ||||||||||||||||||||||
cv::Mat masktmp((int)(crop_region.height), (int)(crop_region.width), CV_8U); | ||||||||||||||||||||||
mask = masktmp; | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
cv::Mat masktmp(capGeo.value().second, capGeo.value().first, CV_8U); | ||||||||||||||||||||||
mask = masktmp; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize similar to example shown above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous not committed suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may possibly be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
||||||||||||||||||||||
|
||||||||||||||||||||||
cv::Mat raw; | ||||||||||||||||||||||
CalcMask ai(s_model.value(), threads, capGeo.value().first, capGeo.value().second); | ||||||||||||||||||||||
int aiw,aih; | ||||||||||||||||||||||
if ( crop_region.width == 0) { | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
aiw=capGeo.value().first; | ||||||||||||||||||||||
aih=capGeo.value().second; | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
} else { | ||||||||||||||||||||||
aiw=crop_region.width; | ||||||||||||||||||||||
aih=crop_region.height; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
CalcMask ai(s_model.value(), threads, aiw, aih); | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
ti.lastns = timestamp(); | ||||||||||||||||||||||
printf("Startup: %ldns\n", diffnanosecs(ti.lastns,ti.bootns)); | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -631,11 +663,15 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
// copy new frame to buffer | ||||||||||||||||||||||
cap.retrieve(raw); | ||||||||||||||||||||||
ti.retrns = timestamp(); | ||||||||||||||||||||||
ai.set_input_frame(raw); | ||||||||||||||||||||||
ti.copyns = timestamp(); | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
if (raw.rows == 0 || raw.cols == 0) continue; // sanity check | ||||||||||||||||||||||
|
||||||||||||||||||||||
if ( crop_region.height) { | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
raw((cv::Rect_<int>)crop_region).copyTo(raw); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
ai.set_input_frame(raw); | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
if (filterActive) { | ||||||||||||||||||||||
// do background detection magic | ||||||||||||||||||||||
ai.get_output_mask(mask); | ||||||||||||||||||||||
|
@@ -646,7 +682,15 @@ int main(int argc, char* argv[]) try { | |||||||||||||||||||||
// - default green (initial value) | ||||||||||||||||||||||
bool canBlur = false; | ||||||||||||||||||||||
if (pbk) { | ||||||||||||||||||||||
if (grab_background(pbk, capGeo.value().first, capGeo.value().second, bg)<0) | ||||||||||||||||||||||
int tw,th; | ||||||||||||||||||||||
if ( crop_region.height ) { | ||||||||||||||||||||||
tw = crop_region.width; | ||||||||||||||||||||||
th = crop_region.height; | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
tw = capGeo.value().first; | ||||||||||||||||||||||
th = capGeo.value().second; | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
if (grab_background(pbk, tw, th, bg)<0) | ||||||||||||||||||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
throw "Failed to read background frame"; | ||||||||||||||||||||||
canBlur = true; | ||||||||||||||||||||||
} else if (blur_strength) { | ||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.